Note that there are some explanatory texts on larger screens.

plurals
  1. POAll nodes in XML using Linq C#
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3872611/use-linq-to-read-all-nodes-from-xml">Use LINQ to read all nodes from XML</a> </p> </blockquote> <p>I am trying to read an XML file using Linq in C# windows application. The sample of the xml string is given below.</p> <pre><code>&lt;Root&gt; &lt;Name&gt;John Doe&lt;/Name&gt; &lt;Data&gt;FBCCF14D504B7B2DBCB5A5BDA75BD93B&lt;/Data&gt; &lt;customer&gt;true&lt;/customer&gt; &lt;Accounts&gt;1&lt;/Accounts&gt; &lt;dataSet&gt; &lt;Type1&gt;Found matching records.&lt;/Type1&gt; &lt;Type2&gt;No matches found.&lt;/Type2&gt; &lt;Type3&gt;Found matching records.&lt;/Type3&gt; &lt;/dataSet&gt; &lt;/Root&gt; </code></pre> <p>I want to display all the data inside the <code>&lt;dataset&gt;</code> tag and <code>&lt;datatag&gt;</code> i want to read <code>&lt;customer&gt;</code> tag as well.</p> <p>I have created a class with members (string type, string status). Where in type i want to store the type1, 2...and in status i want to store what is inside the type node.</p> <p>I am able to accomplish this but in the code i have to give </p> <p><code> type1 = (string)row.Element("type1"), type2=(string)row.Element("type2"), </code> i want to have a generic code in which i dont have to mention every type. In other words i want to read all the child nodes of tag whithout mentioning the tag name. I have spent 2 hours searching for this on google, but haven't found anything yet.</p> <p>Expected output</p> <p>save the information in class object (type and status).</p> <p>And i want to read the customer tag so that i can know whether the person is already a customer</p> <p>Any help will be very much appreciated. </p> <p>Thanks</p> <p><strong>Update</strong></p> <p>According to inputs received from Raphaël Althaus</p> <p>I have the following code:</p> <pre><code>var list = xml.Descendants("dataSet").Elements() .Select(m =&gt; new CustomerInfo { Type = m.Name.LocalName, Value = m.Value }).ToList(); foreach (CustomerInfo item in list) { MessageBox.Show(item.Type+ " "+item.Value); } </code></pre> <p>and for reading the Customer tag i have written more code.</p> <pre><code>var isCustomer = from customer in xmlDoc.Descendants("Root") select new { customer = tutorial.Element("customer").Value, } </code></pre> <p>Can i do both in one query?. Or this method is not so heavy on performance, so i can use this?</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.
 

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