Results 1 to 7 of 7

Thread: [RESOLVED] XML data extraction

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2014
    Posts
    10

    Resolved [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.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jun 2014
    Posts
    10

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2014
    Posts
    10

    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

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Grumpy()
    4.         Dim ds As New DataSet()
    5.         ds.ReadXml("C:\doc.xml")
    6.         MsgBox(ds.Tables(0).Rows(3)("value"))
    7.     End Sub
    8.  
    9. End Class

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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:
    1. Dim item = foo.Elements.FirstOrDefault(Function(x) x.@name = "Container.DeviceId")
    2. Debug.WriteLine(item.@value)

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: [RESOLVED] XML data extraction

    I saw that, but I left it because the example may have not been complete.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width