Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would use the XLINQ classes in System.Xml.Linq (this is the namespace and the assembly you will need to reference). Load the XML into and XDocument:</p> <pre><code>XDocument doc = XDocument.Parse(someString); </code></pre> <p>Next you can either use recursion or a pseudo-recursion loop to iterate over the child nodes. You can choose you child nodes like:</p> <pre><code>//if Directory is tag name of Directory XML //Note: Root is just the root XElement of the document var directoryElements = doc.Root.Elements("Directory"); //you get the idea var fileElements = doc.Root.Elements("File"); </code></pre> <p>The variables <em>directoryElements</em> and <em>fileElements</em> will be IEnumerable types, which means you can use something like a foreach to loop through all of the elements. One way to build up you elements would be something like this:</p> <pre><code>List&lt;MyFileType&gt; files = new List&lt;MyFileType&gt;(); foreach(XElelement fileElement in fileElements) { files.Add(new MyFileType() { Prop1 = fileElement.Element("Prop1"), //assumes properties are elements Prop2 = fileElement.Element("Prop2"), }); } </code></pre> <p>In the example, <em>MyFileType</em> is a type you created to represent files. This is a bit of a brute-force attack, but it will get the job done.</p> <p>If you want to use XPath you will need to <em>using</em> System.Xml.XPath.</p> <hr/> <p><strong><em>A Note on System.Xml vs System.Xml.Linq</em></strong></p> <p>There are a number of XML classes that have been in .Net since the 1.0 days. These live (mostly) in System.Xml. In .Net 3.5, a wonderful, new set of XML classes were released under System.Xml.Linq. I cannot over-emphasize how much nicer they are to work with than the old classes in System.Xml. I would highly recommend them to any .Net programmer and especially someone just getting into .Net/C#. </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