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

Posted on 2007-07-31
Last Modified: 2008-02-01
VS 2005

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

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

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.


Question by:Todd_Anderson
    LVL 96

    Expert Comment

    by:Bob Learned
    Dim document As New XmlDocument()

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

    For Each node As XmlNode In system.ChildNodes
    Next node


    Author Comment


    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

    LVL 96

    Accepted Solution


    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


    Author Comment

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

    Thanks for the help,


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    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…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now