Read XML file using LINQ and show the data in GridView

Akiii
Posted by Akiii under LINQ category on | Points: 40 | Views : 3394
Hi,
Suppose we have a contact.xml file :-

<?xml version="1.0" encoding="utf-8">

<contactdetail>
<contact>
<name>Alex Mercer</name>
<title>dotnet developer</title>
</contact>
<contact>
<name>Sam Fischer</name>
<title>java developer</title>
</contact>
</contactdetail>


Now, we will read the xml file by using LINQ and display the data in gridview :-

XDocument contactfileobj = XDocument.Load(Server.MapPath(contacts.xml));

var contacts = from c in contactfileobj.Descendents("contact")
select new {
name = (string) c.Element("name"),
title = (string) c.Element("title")
};

GridView1.DataSource = contacts;
GridView1.DataBind();


I hope I have made myself clear. Please feel free to give feedback and let me know if there is any mistake.

Thanks and Regards
Akiii

Comments or Responses

Login to post response