Click here to Skip to main content
14,420,468 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

how can i read the xml child nodes by using linq?

Example:

<customer>
<params>
<param>
  <member>
            <name>Challu</name>
            <value>
                <string>SM0109F1</string>
            </value>
          </member>
<member>
            <name>Sheshu</name>
            <value>
                <string>SM0109F2</string>
            </value>
          </member>
<member>
            <name>Basha</name>
            <value>
                <string>SM0109F3</string>
            </value>
          </member>

</param>
</params>
</customer>


In the above i want to read the "basha" and its value by using linq

Thanks& regards,
Basha
Posted
Updated 10-Nov-11 0:59am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

// Create the query
var custs = from c in XElement.Load("Customers.xml").Elements("basha")
          select c ;

// Execute the query
foreach (var customer in custs)
{
     Console.WriteLine(customer);
}

Reference Link :- Reading XML documents using LINQ to XML[^]
   
v2
Comments
inayat basha 16-Nov-11 5:43am
   
We can get the value like this----

var ColumnValues = from member in doc.Descendants("customer").Descendants("params").Descendants("param").Descendants("member")
select new
{
name = member.Element("name").Value,
value = member.Element("value").Value
};

foreach (var structRes in ColumnValues)
{
string ColumnName = structRes.name.ToString();
string ColumnValue = structRes.value.ToString();
}
Rate this:
Please Sign up or sign in to vote.

Solution 2

We can get the value like this----
  var ColumnValues = from member in doc.Descendants("customer").Descendants("params").Descendants("param").Descendants("member")
                              select new
                              {
                                 name = member.Element("name").Value,
                                 value = member.Element("value").Value
                              };

foreach (var structRes in ColumnValues)
        {
            string ColumnName = structRes.name.ToString();
            string ColumnValue = structRes.value.ToString();
}



Regards,
S.Inayat basha
   
v2
Comments
RaviRanjanKr 16-Nov-11 8:55am
   
[Edited]Code is wrapped in Pre Tag[/Edited]
A suggestion :- Always warp your code in Pre tag...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100