Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with vb xml parsing

Posted on 2014-12-23
5
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 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