<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>

[{'answer_id': 26805, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 4, 'selected': True, 'text': '<p><code>//</code> 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.</p>\n\n<p>So you have:</p>\n\n<pre><code>XmlNode stuffChild = stuff.SelectSingleNode(".//stuffChild");\n</code></pre>\n\n<p>which translates to:</p>\n\n<p><strike>xmlNode stuffChild = stuff.SelectSingleNode("self::node()/descendant::stuffChild");</strike></p>\n\n<pre><code>xmlNode stuffChild = stuff.SelectSingleNode("self::node()/descendant-or-self::stuffChild");\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>xmlNode stuffChild = stuff.SelectSingleNode("self::node()/descendant::stuffChild");\n</code></pre>\n\n<p>Also note that if "stuffChild" is a direct descendant of "stuff", you can completely omit the prefixes, and just select "stuffChild".</p>\n\n<pre><code>XmlNode stuffChild = stuff.SelectSingleNode("stuffChild");\n</code></pre>\n\n<p>The <a href="http://www.w3schools.com/xsl/xpath_syntax.asp" rel="nofollow noreferrer">W3Schools</a> tutorial has helpful info in an easy to digest format.</p>\n'}, {'answer_id': 26813, 'author': 'Tom Lokhorst', 'author_id': 2597, 'author_profile': 'https://Stackoverflow.com/users/2597', 'pm_score': 2, 'selected': False, 'text': '<p>The <code>//</code> you use in front of <code>stuffChild</code> means you\'re looking for <code>stuffChild</code> elements, starting from the root.</p>\n\n<p>If you want to start from the current node (decendants of the current node), you should use <code>.//</code>, as in:</p>\n\n<pre><code>stuff.SelectSingleNode(".//stuffChild");\n</code></pre>\n'}, {'answer_id': 26817, 'author': 'Rob Thomas', 'author_id': 803, 'author_profile': 'https://Stackoverflow.com/users/803', 'pm_score': 1, 'selected': False, 'text': '<p>If "stuffChild" is a child node of "stuff", then your xpath should just be:</p>\n\n<pre><code>XmlNode stuffChild = stuff.SelectSingleNode("stuffChild");\n</code></pre>\n'}, {'answer_id': 596498, 'author': 'Azat Razetdinov', 'author_id': 9649, 'author_profile': 'https://Stackoverflow.com/users/9649', 'pm_score': -1, 'selected': False, 'text': '<p>Selecting single node means you need only the first element. So, the best solution is:</p>\n\n<pre><code>XmlNode stuffChild = stuff.SelectSingleNode("descendant::stuffChild[1]");\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/26800', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1965/']