Linq to Xml
Posted by Sudeep Pandey on September 22, 2008
Xml file: student.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<students>
<student>
<name>sudeep</name>
<roll>982102</roll>
<countries>
<country id=”1″>nepal</country>
<country id=”2″>usa</country>
</countries>
</student>
<student>
<name>abhinab</name>
<roll>56780</roll>
<countries>
<country id=”1″>uk</country>
<country id=”2″>japan</country>
</countries>
</student>
<student>
<name>suraj</name>
<roll>43223</roll>
<countries>
<country id=”1″>uae</country>
<country id=”2″>australia</country>
</countries>
</student>
</students>
C# method to extract name
static void LinqtoXml()
{
XElement doc = XElement.Load(“student.xml”);
var name=from stname in doc.Descendants(“name”) where stname.Value.StartsWith(“s”) select stname.Value;
foreach (var sname in name)
{
Console.WriteLine(“name: {0}”, sname);
}
}
output:
name:sudeep
name:suraj