12

I'm using XPath in .NET to parse an XML document, along the lines of:

XmlNodeList lotsOStuff = doc.SelectNodes("//stuff");

foreach (XmlNode stuff in lotsOStuff) {
   XmlNode stuffChild = stuff.SelectSingleNode("//stuffChild");
   // ... etc
}

The issue is that the XPath Query for stuffChild is always returning the child of the first stuff element, never the rest. Can XPath not be used to query against an individual XMLElement?

4 Answers 4

10

// at the beginning of an XPath expression starts from the document root. Try ".//stuffChild". . is shorthand for self::node(), which will set the context for the search, and // is shorthand for the descendant axis.

So you have:

XmlNode stuffChild = stuff.SelectSingleNode(".//stuffChild");

which translates to:

xmlNode stuffChild = stuff.SelectSingleNode("self::node()/descendant::stuffChild");

xmlNode stuffChild = stuff.SelectSingleNode("self::node()/descendant-or-self::stuffChild");

In the case where the child node could have the same name as the parent, you would want to use the slightly more verbose syntax that follows, to ensure that you don't re-select the parent:

xmlNode stuffChild = stuff.SelectSingleNode("self::node()/descendant::stuffChild");

Also note that if "stuffChild" is a direct descendant of "stuff", you can completely omit the prefixes, and just select "stuffChild".

XmlNode stuffChild = stuff.SelectSingleNode("stuffChild");

The W3Schools tutorial has helpful info in an easy to digest format.

4
  • .//foo is not equal to descendant::foo and is generally wrong way of selecting descendant nodes. See stackoverflow.com/questions/453191 Feb 23, 2009 at 0:16
  • Perhaps you missed the ., which sets the context of the following XPath? Feb 27, 2009 at 16:30
  • Please, see documentation for the // abbreviation. .//foo translates to self::node()/descendant-or-self::node()/child::stuffChild Feb 27, 2009 at 20:14
  • You are correct - updating the answer. Note that .// would work for the asker's situation (the child node having a different name than the context node). Mar 6, 2009 at 12:43
2

The // you use in front of stuffChild means you're looking for stuffChild elements, starting from the root.

If you want to start from the current node (decendants of the current node), you should use .//, as in:

stuff.SelectSingleNode(".//stuffChild");
1

If "stuffChild" is a child node of "stuff", then your xpath should just be:

XmlNode stuffChild = stuff.SelectSingleNode("stuffChild");
-1

Selecting single node means you need only the first element. So, the best solution is:

XmlNode stuffChild = stuff.SelectSingleNode("descendant::stuffChild[1]");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.