tags:

views:

104

answers:

3

Say i have the following xml file:

<a>
  <b>
    <c></c>
  </b>
  <b>
    <c></c>
  </b>
</a>

var nodes = doc.SelectNodes("/a/b");

will select the two b nodes.

I then loop these two nodes suchas:

 foreach (XmlNode node in nodes) { }

However, when i call node.SelectNodes("/a/b/c"); It still returns both values and not just the descendants. Is it possible to select nodes only descening from the current node?

+1  A: 

In the foreach loop, you already know that node is a /a/b in the original document - so to get just its c children simply use a relative xpath:

node.SelectNodes("c")
AakashM
+1  A: 

You can use node.SelectSingleNode("C");

J Angwenyi
+1  A: 
/a/b[1]/c

For intance gets the nodelist of all the children of the first b that have the tagname c.

To get the first c as a singleton nodelist use /a/b[1]/c[1]. /a/b/c[1] again returns a nodelist of multiple nodes.

SelectSingleNode is probably misleading, as far as I know, XPath always returns a nodelist, which can contain optionally one node (or even be empty).

//c[1] just selects the first c in the document.

Lajla