Solved

Problem with vb xml parsing

Posted on 2014-12-23
5
153 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 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get sourcecode path 14 65
Filtering a datagrid view 8 46
make control visible based off other control value 7 45
AD Computer Objects in VB .Net 2 21
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…

752 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