Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do all of the searching using XPath.</p> <pre><code>XmlNodeList nodes = doc.SelectNodes("/Menu/MenuItem"); </code></pre> <p>In this case, <code>nodes</code> will contain all of the <code>MenuItem</code> elements that are a child of the top-level <code>Menu</code> element.</p> <pre><code>XmlNodeList nodes = doc.SelectNodes("/Menu/MenuItem | /Menu/MenuItem/MenuItem"); </code></pre> <p>In this case, <code>nodes</code> will contain all <code>MenuItem</code> elements that are children either of <code>Menu</code> or of a <code>MenuItem</code> that's a child of <code>Menu</code>.</p> <p>You can also find all <code>MenuItem</code> nodes that have no more than 1 <code>MenuItem</code> ancestor:</p> <pre><code>XmlNodeList nodes = doc.SelectNodes("//MenuItem[count(ancestor::MenuItem) &amp;lt; 2]"); </code></pre> <p>Or you can just find all <code>MenuItem</code> nodes:</p> <pre><code>XmlNodeList nodes = doc.SelectNodes("//MenuItem"); </code></pre> <p>However you choose to select them, processing the elements works the same way:</p> <pre><code>foreach (XmlElement elm in nodes) { myList.Add(new[] { elm.GetAttribute("Header"), elm.GetAttribute("Name") }); } </code></pre> <p>So this suggests a way that you could encapsulate this as a method with the signature you suggest:</p> <pre><code>public static List&lt;IEnumerable&lt;string&gt;&gt;ReadXML( string filename, string elementName, IEnumerable&lt;string&gt; attributes, bool searchSubNodes) { XmlDocument d = new XmlDocument(); d.Load(filename); string xpath = searchSubNodes ? "//" + elementName : "/*/elementName"; List&lt;IEnumerable&lt;string&gt;&gt; results = new List&lt;IEnumerable&lt;string&gt;&gt;(); foreach (XmlElement elm in d.SelectNodes(xpath)) { var values = from name in attributes select elm.GetAttribute(name); result.Add(values.ToArray()); } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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