HOW TO GET PERTICULAR RECORD IN XML FILE USING LINQ.

James Smith

Total Post:48

Points:336
Posted by  James Smith
C# 
C#
 1822  View(s)
Ratings:
Rate this:
Hi,
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.
<root>
<Employee>
<Name>John</Name>
</Employee>
<Employee>
<Name>Jack</Name>
</Employee>
<Employee>
<Name>Tori</Name>
</Employee>
</root>

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

Thanks.
  1. Awadhendra Tiwari

    Post:126

    Points:882
    Re: How to get perticular record in XML file using LINQ.

    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,
                                          }).FirstOrDefault();
                    if (record != null)
                    {
                             //Display record.
                    }

    This code might be useful for you.
    Thanks.

  1. Jenry Hock

    Post:18

    Points:126
    Re: How to get perticular record in XML file using LINQ.

    Thanks Awadhenra,
    This code is work for me.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!