Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>EDIT:</p> <p>To use XPath to select all nodes that have a given node name AND two given attributes use the following XPath expression (I don't know if there are better ways to do it but this works):</p> <pre><code>//NodeName[@FirstAttributeName][@SecondAttributeName] </code></pre> <p>In code you could do it like that (for the MenuItems from your example). I updated the code to make the namespace you put in your document known under the prefix "x". Also the XPath expression has been updated to use that prefix before the node.</p> <pre><code>XPathDocument doc = new XPathDocument("data2.xml"); XPathNavigator nav = doc.CreateNavigator(); XmlNamespaceManager mgr = new XmlNamespaceManager(nav.NameTable); mgr.AddNamespace("default", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); XPathNodeIterator iterator = nav.Select("//default:MenuItem[@Header][@Name]", mgr); while (iterator.MoveNext()) { Console.Write(iterator.Current.GetAttribute("Header", "")); Console.Write(iterator.Current.GetAttribute("Name", "")); Console.Write(Environment.NewLine); } </code></pre> <p>=====</p> <p>Do you have to use XPath? You could just create classes for Menu/MenuItem that map to your XML like this:</p> <pre><code>public class Menu { public int Id { get; set; } public string Name { get; set; } public List&lt;MenuItem&gt; SubItems { get; set; } } public class MenuItem { public string Header { get; set; } public string Name { get; set; } public List&lt;MenuItem&gt; SubItems { get; set; } } </code></pre> <p>And then have an <a href="http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx" rel="nofollow noreferrer">XMLSerializer</a> do the work for you (it can do both, reading and writing). You could afterwards apply filtering to the in-memory lists (e.g. using Linq-To-Objects).</p> <p>If you want to do it using XPath this <a href="http://www.codeproject.com/KB/cpp/myXPath.aspx" rel="nofollow noreferrer">Codeproject article</a> might be helpful.</p>
 

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