XPath Iterating through nodes with C#

Posted on 2006-06-08
Last Modified: 2008-01-09
I'm using C# and reading an XML document like the following (example):


I'm using the xPathNavigator and xPathNodeIterator to go through each node. The problem is I don't always know what nodes will be in the <it> node. So I need a way to iterate through the nodes and just add the node name and value to an array. I've almost got it however when I use the xPathNodeIterator and search all descendants it returns "falsetrue" for the <jumbo> node - apparently reading the value of this node concatenates the two child node values.

Is there a way around this? I want to iterate through each node and return just the node name and value without adding an element that has child elements.
Question by:SuperGhosty
    LVL 52

    Expert Comment

    by:Carl Tawn
    Can you post your code ? Saves me having to knock up my own sample ;o)
    LVL 3

    Author Comment

    Yea I was at work when I posted so didn't have it available, but here you go, this is what I use to iterate through each element:

            private void Iterate(XPathNavigator xpNav)
                    XPathNodeIterator nodes = xpNav.Select("/it/prc/item");
                    XPathNavigator nodesNavigator = nodes.Current;
                    XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);

                    while (nodes.MoveNext())
                        if (!otherNodes.ContainsKey(nodes.Current.Name))
                            otherNodes.Add(nodes.Current.Name, nodes.Current.Value);
    LVL 52

    Accepted Solution

    Do you want to add every node to (I assume your using a HashTable), or only the ones with a Text element ?

    The problem you have at the moment is that the Value of any node is the sum text of all of its child nodes. So, you basically need to check if the current nodes has any child nodes and decide whether to add the current node or not.
    LVL 3

    Author Comment

    Anything with text. And yes you're correct, for the example above I am getting one item being the sum of all the child nodes (what i dont want) and then the individual child nodes (what i do want).

    I tried adding a check for child nodes using the following codes, however it doesn't seem to do anything - I get the same results:

            if (nodesText.Current.HasChildren) {
            } else {
                //add to hashtable

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now