Solved

Problem with vb xml parsing

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
start a process from a service 3 33
Footer for each row on Gridview 2 33
Please explain purpose of GZIP 4 35
vb.net Configuration, settings file location? 8 31
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

838 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