-
Jun 20th, 2014, 06:37 AM
#1
Thread Starter
New Member
[RESOLVED] XML data extraction
Hi,
I have been trying to extract some data from XML and seem to been returning nothing
HTML Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<coreProperties xmlns="http://www.myserver.com">
<coreProperty name="Common.ApplicationHome" value="1" />
<coreProperty name="Common.InstallationHome" value="2" />
<coreProperty name="Container.DeviceId" value="3"/>
<coreProperty name="Service.DeviceID" value="4"/>
<coreProperty name="Service.DeviceType" value="5" />
<coreProperty name="Service.LocationID" value="6" />
</coreProperties>
I'm trying to return the value for Container.DeviceId (I want to return the value 3). Ive gone through various permutations, my latest is.
Code:
Dim doc As XmlDocument = New XmlDocument()
Dim myXMLAs String = "C:\my.xml"
doc.Load(myXML)
Dim root As XmlNode = doc.DocumentElement
Dim nodeDeviceId As XmlNode = root.SelectSingleNode("/coreProperties/coreProperty[value='']")
Dim deviceID As String = nodeDeviceId.Attributes.ItemOf("value").InnerText
When debugging my Autos section returns the value Nothing.
-
Jun 20th, 2014, 09:34 AM
#2
Thread Starter
New Member
Re: XML data extraction
Edit:
This line actually reads
Dim nodeDeviceId As XmlNode = root.SelectSingleNode("/coreProperties/coreProperty[@name='Container.DeviceId']")
I copied the wrong code in, but still doesn't work either way
-
Jun 20th, 2014, 11:26 AM
#3
Thread Starter
New Member
Re: XML data extraction
Well as no one got back to me, very helpful!. I managed to work it out.
Code:
Dim Doc As XmlDocument = New XmlDocument()
Doc.Load(enactorXML)
Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(Doc.NameTable)
nsmgr.AddNamespace("core", "http://www.myserver.com")
Dim root As XmlNode = Doc.DocumentElement
Dim nodelist As XmlNodeList
nodelist = root.SelectNodes("//core:coreProperties/core:coreProperty[@name='Container.DeviceId']", nsmgr)
Dim deviceID As String = nodelist.Item(0).Attributes.ItemOf("value").InnerText
-
Jun 20th, 2014, 02:37 PM
#4
Re: [RESOLVED] XML data extraction
Just remember we are volunteers. We don't get paid or owe you guys any thing. You could use a dataset
vb Code:
Public Class Form1 Private Sub Grumpy() Dim ds As New DataSet() ds.ReadXml("C:\doc.xml") MsgBox(ds.Tables(0).Rows(3)("value")) End Sub End Class
-
Jun 21st, 2014, 06:17 AM
#5
Re: XML data extraction
Code:
Dim foo As XElement = <coreProperties xmlns="http://www.myserver.com">
<coreProperty name="Common.ApplicationHome" value="1"/>
<coreProperty name="Common.InstallationHome" value="2"/>
<coreProperty name="Container.DeviceId" value="3"/>
<coreProperty name="Service.DeviceID" value="4"/>
<coreProperty name="Service.DeviceType" value="5"/>
<coreProperty name="Service.LocationID" value="6"/>
</coreProperties>
Dim bar As IEnumerable(Of XElement) = From core As XElement In foo.Elements
Where core.@name = "Container.DeviceId" Select core
Debug.WriteLine(bar(0).@value)
-
Jun 21st, 2014, 02:16 PM
#6
Re: [RESOLVED] XML data extraction
Select core is redundant because where selects the local range variable. In actual fact where can be replace with short circuiting FirstOrDefault
vb Code:
Dim item = foo.Elements.FirstOrDefault(Function(x) x.@name = "Container.DeviceId") Debug.WriteLine(item.@value)
-
Jun 21st, 2014, 02:23 PM
#7
Re: [RESOLVED] XML data extraction
I saw that, but I left it because the example may have not been complete.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|