Home > DeveloperSection > Forums > How to get perticular record in XML file using LINQ.
James Smith

Total Post:48

Posted on    August-12-2011 2:03 AM

 C# C# 

 2 Reply(s)
 1461  View(s)
Rate this:
I have a small problem, I have a xml file which contains some record, I want to search that record using Linq and if that record does not exists then It return null value.
The format of XML file is as follows.

How can I perform this task....
Help needed........


Awadhendra Tiwari

Total Post:126

Posted on    August-12-2011 2:14 AM

Hi James,

Use following link to solve your problem,
public class Employee
    public string Name { get; set; }

at click of button event write following code.

XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("Team\\Team.xml"));
                TeamDetails record = (from r in data.Elements("root").Elements("Team")
                                      where r.Element("Id").Value.Equals(Request.QueryString["Id"].ToString())
                                      select new TeamDetails()
                                          Name = r.Element("Name").Value,
                if (record != null)
                         //Display record.

This code might be useful for you.

Jenry Hock
Jenry Hock

Total Post:18

Posted on    October-03-2011 1:00 AM

Thanks Awadhenra,
This code is work for me.

Don't want to miss updates? Please click the below button!

Follow MindStick