?
Solved

How do I iterate through child nodes in an XML file?

Posted on 2007-07-31
4
Medium Priority
?
520 Views
Last Modified: 2008-02-01
VS 2005
VB

How do I iterate through child nodes in an XML file?  Here is an example of my file format.

    <?xml version="1.0"?>
    <Systems>
        <System>
            <ChildNode1>Child node 11 info here</ChildNode1>
            <ChildNode2>Child node 21 info here</ChildNode2>
            <ChildNode3>Child node 31 info here</ChildNode3>
            ...
        </System>
        <System>
            <ChildNode1>Child node 12 info here</ChildNode1>
            <ChildNode2>Child node 22 info here</ChildNode2>
            <ChildNode3>Child node 32 info here</ChildNode3>
            ...
        </System>
        ...
</Systems>

I know how to iterate through the first level (System) nodes but I can't figure out how to iterate though the child nodes level.

Thanks,

Todd
0
Comment
Question by:Todd_Anderson
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19602253
Dim document As New XmlDocument()
document.Load(fileName)

Dim system As XmlNode = document.SelectSingleNode("//System")

For Each node As XmlNode In system.ChildNodes
Next node

Bob
0
 

Author Comment

by:Todd_Anderson
ID: 19602953
Bob,

Thanks for the example.  Since I need to iterate through all child nodes this is what I have come up with.  Am I on the right track?

        Dim Nodes As Xml.XmlNodeList = document.DocumentElement.SelectNodes("/Systems/System")
        Dim Node As XmlNode = document.SelectSingleNode("//System")
        Dim ChildNode As XmlNode

        For Each Node In Nodes
            If Not Node Is Nothing Then
                For Each ChildNode In Node
                    If Not ChildNode Is Nothing Then
                        Do My Stuff Here
                    End If
                Next ChildNode
            End If
        Next Node

Todd
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 19603230
Todd:

1) //System is an XPath shortcut to an element no matter where it is in the XML hierarchy, so you don't need /Systems/System, or this line:

        Dim Nodes As Xml.XmlNodeList = document.DocumentElement.SelectNodes("/Systems/System")

2) You don't have to check for Not Node Is Nothing, because it will never be true in the loop.

3)  I showed you how to get at a single System element, but for all elements, it would be like this:

Dim systemList As XmlNodeList = document.SelectNodes("//System")

For Each system As XmlNode In systemList
   For Each child As XmlNode in system.ChildNodes
   Next child
Next node

Bob
0
 

Author Comment

by:Todd_Anderson
ID: 19604182
That's way better.  I felt like I was going in circles.  That is clean, simple and makes sense!

Thanks for the help,

Todd
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

839 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