• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Problem with vb xml parsing

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
RickCooper
Asked:
RickCooper
  • 3
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
RickCooperAuthor Commented:
Hi Carl,

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


Thanks
0
 
RickCooperAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
RickCooperAuthor Commented:
Thanks Carl.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now