Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Do you assume that the <code>nodeSet</code> contains the “Root” element because the debugger displays its string representation as <code>Position=0, Current={Root}</code>? That may be misleading; the reason that the “Root” element appears is because the <code>XPathNodeIterator</code> (which is the actual type returned by <code>Evaluate</code> in your case) has not yet been positioned on the first node resulting from the query. Per the MSDN documentation for <a href="http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnodeiterator.aspx" rel="nofollow"><code>XPathNodeIterator</code></a>:</p> <blockquote> <blockquote> <p>An <code>XPathNodeIterator</code> object returned by the <code>XPathNavigator</code> class is not positioned on the first node in a selected set of nodes. A call to the <code>MoveNext</code> method of the <code>XPathNodeIterator</code> class must be made to position the <code>XPathNodeIterator</code> object on the first node in the selected set of nodes.</p> </blockquote> </blockquote> <p>You can retrieve your path items by iterating over your <code>nodeSet</code>:</p> <pre><code>XDocument document = XDocument.Load("someXML"); XPathNavigator navigator = document.CreateNavigator(); XPathExpression expression = XPathExpression.Compile("//*[@test]"); XPathNodeIterator nodeSet = (XPathNodeIterator)navigator.Evaluate(expression); foreach (XPathItem item in nodeSet) { // Process item here } </code></pre> <p>Or, if you want them converted to an array:</p> <pre><code>XDocument document = XDocument.Load("someXML"); XPathNavigator navigator = document.CreateNavigator(); XPathExpression expression = XPathExpression.Compile("//*[@test]"); XPathNodeIterator nodeSet = (XPathNodeIterator)navigator.Evaluate(expression); XPathItem[] items = nodeSet.Cast&lt;XPathItem&gt;().ToArray(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload