Read XML file using LINQ and show the data in GridView

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

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

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

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;

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

Comments or Responses

Login to post response