Solved

Problem with vb xml parsing

Posted on 2014-12-23
5
137 Views
Last Modified: 2014-12-24
I am trying to parse the test xml file which is attached using the following code:
  Try
            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
            Dim m_node As XmlNode
            Dim m_nodelist1 As XmlNodeList
            Dim m_node1 As XmlNode
            'Create the XML Document
            m_xmld = New XmlDocument()
            'Load the Xml file
            m_xmld.Load("C:\XMLTest\test.xml")
            'Get the list of name nodes 
            m_nodelist = m_xmld.SelectNodes("/features/compound_hole")
            'Loop through the nodes
            For Each m_node In m_nodelist
                'Get the hole Attribute Value
                Dim holeAttribute = m_node.Attributes.GetNamedItem("name").Value
                Dim hole As String
                hole = holeAttribute
                'Get the list of hole nodes 
                m_nodelist1 = m_xmld.SelectNodes("/features/hole_defs")
                'Loop through the nodes
                For Each m_node1 In m_nodelist1
                    'Get the hole Attribute Value
                    Dim hole_defsAttribute = m_node1.Attributes.GetNamedItem(hole).Value
                    Dim hole_type As String
                    hole_type = hole_defsAttribute
                    Dim info As String
                    info = hole & " " & hole_type
                    MsgBox(info)
                Next
            Next
        Catch errorVariable As Exception
            'Error trapping
            Console.Write(errorVariable.ToString())
        End Try
    

Open in new window


I am trying to match the hole_def id to hole_definition  and return the following to each compound-hole
Name, depth_type, depth, diameter.

The code below works on a simple xml file atactched (family.xml)  and I have tried to modify it to work with the more complex xml file.

Working Code:
   Try
            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
            Dim m_node As XmlNode
            'Create the XML Document
            m_xmld = New XmlDocument()
            'Load the Xml file
            m_xmld.Load("C:\XMLTest\family.xml")
            'Get the list of name nodes 
            m_nodelist = m_xmld.SelectNodes("/family/name")
            'Loop through the nodes
            For Each m_node In m_nodelist
                'Get the Gender Attribute Value
                Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value
                'Get the firstName Element Value
                Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText
                'Get the lastName Element Value
                Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText
                'Write Result to the Console
                Dim info As String
                info = genderAttribute & " " & firstNameValue & " " & lastNameValue
                MsgBox(info)

            Next
        Catch errorVariable As Exception
            'Error trapping
            Console.Write(errorVariable.ToString())
        End Try

Open in new window

family.xml
test.xml
0
Comment
Question by:RickCooper
  • 3
  • 2
5 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40515518
I might have misunderstood what you are trying to do, but give this a go:
        Dim doc As New XmlDocument()
        doc.Load("C:\temp\test.xml")

        Dim ns As New XmlNamespaceManager(doc.NameTable)
        ns.AddNamespace("x", "x-schema:Features.xml")

        Dim nodeList As XmlNodeList = doc.SelectNodes("/x:features/x:featureset/x:compound_hole", ns)
        For Each node As XmlNode In nodeList

            '// get the hole_definition attribute
            Dim hole As String = node.Attributes("hole_definition").Value

            '// find the matching hole_def node
            Dim holeNode As XmlNode = doc.SelectSingleNode("/x:features/x:hole_defs/x:hole_def[@id=""" & hole & """]", ns)
            Dim holeType As String = holeNode.Attributes("hole_type").Value

            MsgBox(hole & " " & holeType)

        Next

Open in new window

0
 
LVL 1

Author Comment

by:RickCooper
ID: 40516116
Hi Carl,

It brings back the Hole and Hole Type but not the depth and diameter from the Geometry part.


Thanks
0
 
LVL 1

Author Comment

by:RickCooper
ID: 40516125
Hi Carl,

 I have added the following code but it only returns the first Geometry values.
Hole2 has three geometry values.
 '// find the matching Geometry node
            Dim geometryNode As XmlNode = doc.SelectSingleNode("/x:features/x:hole_defs/x:hole_def[@id=""" & hole & """]/x:hole/x:geometry", ns)
            Dim depthType As String = geometryNode.Attributes("depth_type").Value
            Dim depth As String = geometryNode.Attributes("depth").Value
            Dim diameter As String = geometryNode.Attributes("diameter").Value


            MsgBox(hole & " " & holeType & " " & depthType & " " & depth & " " & diameter)

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40516158
If there are multiple entries then you need to use SelectNodes rather than SelectSingleNode, and then loop over the results. You can also do the XPath lookup from the context node rather than the root of the document:
        Dim doc As New XmlDocument()
        doc.Load("C:\temp\test.xml")

        Dim ns As New XmlNamespaceManager(doc.NameTable)
        ns.AddNamespace("x", "x-schema:Features.xml")

        Dim nodeList As XmlNodeList = doc.SelectNodes("/x:features/x:featureset/x:compound_hole", ns)
        For Each node As XmlNode In nodeList

            '// get the hole_definition attribute
            Dim hole As String = node.Attributes("hole_definition").Value

            '// find the matching hole_def node
            Dim holeNode As XmlNode = doc.SelectSingleNode("/x:features/x:hole_defs/x:hole_def[@id=""" & hole & """]", ns)
            Dim holeType As String = holeNode.Attributes("hole_type").Value

            MsgBox(hole & " " & holeType)

            '// grab all Geometry nodes starting at the current hole_def node
            Dim geometryNodes As XmlNodeList = holeNode.SelectNodes("x:hole/x:geometry", ns)
            For Each geoNode As XmlNode In geometryNodes
                MsgBox(geoNode.Attributes("pitch").Value & " " & geoNode.Attributes("diameter").Value)
            Next

        Next

Open in new window

0
 
LVL 1

Author Closing Comment

by:RickCooper
ID: 40516185
Thanks Carl.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now