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

<pre class="lang-cs prettyprint-override"><code>XmlNodeList lotsOStuff = doc.SelectNodes("//stuff");

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

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

```
//
```
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.