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

XPath Iterating through nodes with C#

I'm using C# and reading an XML document like the following (example):

<it>
    <abc>123</abc>
    <stuff>0</stuff>
    <jumbo>
        <big>false</big>
        <large>true</large>
    </jumbo>
</it>

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.
0
SuperGhosty
Asked:
SuperGhosty
  • 2
  • 2
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Can you post your code ? Saves me having to knock up my own sample ;o)
0
 
SuperGhostyAuthor Commented:
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);
                    }
                }
        }
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
SuperGhostyAuthor Commented:
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) {
            //skip
        } else {
            //add to hashtable
        }
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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