tags:

views:

1460

answers:

2

i have this problem to find a particular xml node l have post this problem on stackoverflow and some nice fellows suggested xpath.

I am an xml newbie . please I need an c# code to find the parent , parent, parent as (great grand parent) then the first child ,lastchild , lastchild . the code have to iterate up the tree and down again. Be looking at a lot of xpath tutorials online.

I discovered that the path tend to be specific to a particular node already existing . The program that I need will not get no particular named node because at each pass a new node will be add to the xml tree. The long and short of it all is that I need find the node base on it’s position away from the current node

i meant finding the parent parent parent of a currentnode (great grand parentnode )then find the first child then find the lastchild lastchild

keepkind of currentnode.parentnode.parentnode.parentnode.firstchild.lastchild.lastchild;

using xpath C#

+3  A: 

Let's say that you have an XmlNode instance called node to start with. Then the following code will give you the last child of the last child of the first child of the great grand parent of that node:

XmlNode wantedNode = node.ParentNode.ParentNode.ParentNode.FirstChild.LastChild.LastChild;

Note that there are so many things that can go wrong with this code. If any of the referenced nodes happen to be null, you have a NullReferenceException coming. So you will want to make a null check at each level:

XmlNode wantedNode;

if (node.ParentNode != null && node.ParentNode.ParentNode != null /* and so on through the full path */)
{
    wantedNode = node.ParentNode.ParentNode.ParentNode.FirstChild.LastChild.LastChild;
}

Let's examine this with a more concrete example. Assume we have the following Xml document:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <greatgrandparent>
    <grandparent>
      <parent id="1">
        <child somevalue="3"></child>
      </parent>
      <parent id="2">
        <child somevalue="4"></child>
        <child somevalue="5"></child>
      </parent>
    </grandparent>
  </greatgrandparent>
</root>

If I understand your question right, if we start from the node <child somevalue="3"></child> we want to navigate to <child somevalue="5"></child>. The code sample above will do that. However, as mentioned, it is prone to giving exceptions if not all expected nodes are present.

Even though you said that you want c# code rather than XPath, in this case I feel that XPath is the way to go. There are a number of ways to solve this. For instance, if the nodes have different tag names (as in my sample document), you can instead do like this:

XmlNode wantedNode = node.SelectSingleNode("ancestor::greatgrandparent/grandparent[position()=1]/parent[position()=last()]/child[position()=last()]");
if (wantedNode != null)
{
    // the path was found
}

This is of course assuming that node is not null, but a valid XmlNode instance.

Breakdown of the XPath expression:

  • ancestor::greatgrandparent -> this will locate any node named "greatgrandparent" that is located anywhere upwards in the hierarchy (so any parent, grand parent and so on)
  • /grandparent[position()=1] -> the first child node named "grandparent"
  • /parent[position()=last()] -> the last child node named "parent"
  • /child[position()=last()] -> the last child node named "child"

If you want to read some about how XPath axes work, there is some information on w3schools.com.

Fredrik Mörk
hi fredrik that did not work for me i kept getting exception message
Spikie
yes dude u readed me right!!! . Thanks a lot one more thing please explain this "ancestor::greatgrandparent/grandparent[position()=1]/parent[position()=last()]/child[position()=last()]");"ancestor i understood that! what are the rest stand for thank fredrik u have been a great help
Spikie
I added a step by step explanation of the xpath expression in the end.
Fredrik Mörk
i have to accpet ur answer thank a lot of the help u
Spikie
+1  A: 

All the question you are asking are answered by the XmlNode class. It has properties called ParentNode, FirstChild and LastChild which each return another XmlNode.

To do the same thing in XPath you can use the ".." abbreviation to get a nodes parent, and "*[position()=1]" or "*[position()=last()]" to get the first and last child, e.g.:

XmlNode foundNode = node.SelectSingleNode("../../../*[position()=1]/*[position()=last()]/*[position()=last()]");

(Notes: ".." is an abbreviation of the parent::* axis, and "*" is an abbreviation of the "child::*" axis)

samjudson
thanks samjudson can u be more Elaborate please . i think u are hitting the point
Spikie
Elaborate how? What do you not understand?
samjudson