Apr
25th | 2008

LINQ to XML in 3 easy steps

Filed under .Net, C# | Posted by Shahar A

Linq to xml provides an easy query interface for XML files. In the following example I will demonstrate how to use it for reading and writing data from/to xml file, using the file for persistency maintaining a list of objects. This can be used for storing application settings, storing persistent objects or any other data needs to be saved. Follow the 3 steps to get the picture and see how easy it is: 1. Define the Data Entity 2. Create XML Data File 3. Load/Save the Data…

1. Define the Data Entity

In this example we’ll be dealing with patients, so lets define a Patient class with some attributes:

class Patient
{
    public string EMail { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

2. Create XML Data File

<?xml version=“1.0″ encoding=“utf-8″ ?>
<Patients>
  <Patient EMail=“LeBron@James.com”>
    <FirstName>LeBron</FirstName>
    <LastName>James</LastName>
  </Patient>
  <Patient EMail=“Kobe@Bryant.com”>
    <FirstName>Kobe</FirstName>
    <LastName>Bryant</LastName>
  </Patient>
  <Patient EMail=“Allen@Iverson.com”>
    <FirstName>Allen</FirstName>
    <LastName>Iverson</LastName>
  </Patient>
</Patients>

3. Load/Save the data

I Inherited the List<Patient> and add Load/Save methods to it:

class PatietList : List<Patient>
{
    public void Load(string xmlFile)
    {
        XDocument doc = XDocument.Load(xmlFile);     

        var query = from xElem in doc.Descendants(“Patient”)
                    select new Patient
                    {
                        EMail = xElem.Attribute(“EMail”).Value,
                        FirstName = xElem.Element(“FirstName”).Value,
                        LastName = xElem.Element(“LastName”).Value,
                    };     

        this.Clear();
        AddRange(query);
    }     

    public void Save(string xmlFile)
    {
        XElement xml = new XElement(“Patients”,
                        from p in this
                        select new XElement(“Patient”,
                            new XAttribute(“EMail”, p.EMail),
                            new XElement(“FirstName”, p.FirstName),
                            new XElement(“LastName”, p.LastName)));     

        xml.Save(xmlFile);     

    }     

}

See how easy this is? get familiar with the XDocument, XElement and XAttribute classes.

Another thing you could do is to move load/save knowledge into the Data object:

class Patient
{
    …
    public Patient(XElement xElement)
    {
        EMail = xElement.Attribute(“EMail”).Value;
        FirstName = xElement.Element(“FirstName”).Value;
        LastName = xElement.Element(“LastName”).Value;
    }     

    public XElement XElement
    {
        get
        {
            return new XElement(“Patient”,
                    new XAttribute(“EMail”, EMail),
                    new XElement(“FirstName”, FirstName),
                    new XElement(“LastName”, LastName));
        }
    }
    …
}

and than the load/save query looks like this:

public void Load(string xmlFile)
{
    …
    var query = from xElem in doc.Descendants(“Patient”)
                select new Patient(xElem);
    …     

public void Save(string xmlFile)
{
    …
    XElement xml = new XElement(“Patients”,
                    from p in this
                    select p.XElement);

Tags: , , , , , ,

6 Responses to “LINQ to XML in 3 easy steps”



  1. By Sam on Apr 29, 2008 | Reply

    Linq is so neat. I bought a book on it though and the book isn’t really as good as many online resources like this one. Thanks for adding your knowledge to the community’s database.

  1. 5 Trackback(s)

  2. Apr 25, 2008: LINQ to XML in 3 easy steps « Rams On It - .NET
  3. Apr 28, 2008: Weekly Link Post 39 « Rhonda Tipton’s WebLog
  4. Apr 28, 2008: Wöchentliche Rundablage: ASP.NET MVC, Silverlight 2, jQuery, CSS, C#… | Code-Inside Blog
  5. Apr 28, 2008: Weekly Links: ASP.NET MVC, Silverlight 2, jQuery, CSS, C#… | Code-Inside Blog International
  6. May 8, 2008: Clonopin     » LINQ to XML in 3 easy steps

Post a Comment

Search Dev102