Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.aspx" rel="noreferrer">XmlNamespaceManager</a> in your call to <a href="http://msdn.microsoft.com/en-us/library/h0hw012b.aspx" rel="noreferrer">SelectSingleNode()</a> since your XML does contain a namespace on it:</p> <pre><code>var doc = new XmlDocument(); doc.Load("example.xml"); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("anyname", "http://tempuri.org/zitem.xsd"); foreach (XmlNode node in doc.SelectNodes("//anyname:ITEM", ns)) { Console.WriteLine(node.Attributes["id"].Value); } </code></pre> <p>That's why you get no result.</p> <p>The difference from my code to yours is that I am using <code>//</code> so instead of starting at the root of a document, a double forward slash <code>//</code> indicates to an <code>XPath</code> evaluator to look anywhere in an XML document.</p> <p>Here is my <code>example.xml</code> as sample:</p> <pre><code>&lt;root&gt; &lt;items&gt; &lt;ITEM id="1" name="bleh=" /&gt; &lt;ITEM id="2" name="bleh=" /&gt; &lt;ITEM id="3" name="bleh=" /&gt; &lt;ITEM id="4" name="bleh=" /&gt; &lt;ITEM id="5" name="bleh=" /&gt; &lt;ITEM id="6" name="bleh=" /&gt; &lt;ITEM id="7" name="bleh=" /&gt; &lt;ITEM id="8" name="bleh=" /&gt; &lt;/items&gt; &lt;/root&gt; </code></pre> <p>And here is how I am reading it:</p> <pre><code>var doc = new XmlDocument(); doc.Load("example.xml"); foreach (XmlNode node in doc.SelectNodes("//ITEM[@id]")) { Console.WriteLine(node.Attributes["id"].Value); } </code></pre> <p>With single slash, the above <code>XPath</code> would look like this:</p> <pre><code>/root/items/ITEM </code></pre> <p>I am also using <code>[@id]</code> to ensure that the <code>ITEM</code> element have an <code>ID</code> attribute but that is not necessary if you know they all have an <code>ID</code> attribute.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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