July 28, 2011

Transform object to xml using linq

Using Linq, it’s easily possible to query various types of data like sql database, xml, objects like DataTable, ArrayList. It’s also possible to transform data into other formats like xml using linq. In this article, I will demonstrate how to query an System.Collections.ArrayList collection and then to save the data in xml format.

At first, I will create an ArrayList object and add Person objects to it. The code to do is below.

Adding objects to ArrayList
  1. ArrayList list = new ArrayList();
  2. list.Add(new Person { FirstName = "Scooby", LastName = "Doo", DateOfBirth= new DateTime(2000, 12, 31), Height=160 });
  3. list.Add(new Person { FirstName = "Bugs", LastName = "Bunny", DateOfBirth = new DateTime(1999, 2, 7), Height = 170 });
  4. list.Add(new Person { FirstName = "Peter", LastName = "Pan", DateOfBirth = new DateTime(2001, 10, 31), Height = 150 });

The Person class has 4 public properties of different data types – string, DateTime and int as shown below.

Person class
  1. using System;
  2. namespace WebApplication1.CSClass
  3. {
  4.     public class Person
  5.     {
  6.         public string FirstName { get; set; }
  7.         public string LastName { get; set; }
  8.         public DateTime DateOfBirth { get; set; }
  9.         public int Height { get; set; }
  10.  
  11.         public override string ToString()
  12.         {
  13.             return FirstName + " " + LastName;
  14.         }
  15.     }
  16. }

Person objects are added to the ArrayList list which can be queried using linq as shown in my previous post on query ArrayList using Linq. While querying the list, System.Xml.Linq.XElement objects can be created which can be saved as xml using System.Xml.Linq.XDocument’s Save() method. The code to show this is below.

Transform ArrayList to xml
  1. private void TransformToXml()
  2. {
  3.     ArrayList list = new ArrayList();
  4.     list.Add(new Person { FirstName = "Scooby", LastName = "Doo", DateOfBirth= new DateTime(2000, 12, 31), Height=160 });
  5.     list.Add(new Person { FirstName = "Bugs", LastName = "Bunny", DateOfBirth = new DateTime(1999, 2, 7), Height = 170 });
  6.     list.Add(new Person { FirstName = "Peter", LastName = "Pan", DateOfBirth = new DateTime(2001, 10, 31), Height = 150 });
  7.  
  8.     var q = from Person p in list
  9.             orderby p.FirstName
  10.             orderby p.LastName
  11.             select p;
  12.  
  13.     var query = new XElement("Persons", from Person p in list
  14.                                 select new
  15.                                 XElement("Person",
  16.                                 new XElement("FirstName", p.FirstName),
  17.                                 new XElement("LastName", p.LastName),
  18.                                 new XElement("DateOfBirth", p.DateOfBirth),
  19.                                 new XElement("Height", p.Height)
  20.                                 )
  21.                                 );
  22.  
  23.     XDocument doc = new XDocument(query);
  24.     doc.Save(Server.MapPath("transform.xml"));
  25.  
  26.     var query2 = new XElement("Persons", from Person p in list
  27.                                 select new
  28.                                 XElement("Person",
  29.                                 new XAttribute("FirstName", p.FirstName),
  30.                                 new XAttribute("LastName", p.LastName),
  31.                                 new XAttribute("DateOfBirth", p.DateOfBirth),
  32.                                 new XAttribute("Height", p.Height)
  33.                                 )
  34.                                 );
  35.  
  36.     XDocument doc2 = new XDocument(query2);
  37.     doc2.Save(Server.MapPath("transform2.xml"));
  38. }

As can be seen, the ArrayList object is queried for Person objects. This is not really needed for the code to work but is an example on how objects can be queried from ArrayList.

Using another query, “query”, XElement objects are created. The names of the XElement – that is, the names of the xml elements are arbitrary and the value of the elements are obtained from the Person object. The XElement objects are then added to XDocument object and saved as xml using XDocument.Save() method. The resultant xml looks like below.

Xml from Linq to ArrayList
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Persons>
  3.   <Person>
  4.     <FirstName>Scooby</FirstName>
  5.     <LastName>Doo</LastName>
  6.     <DateOfBirth>2000-12-31T00:00:00</DateOfBirth>
  7.     <Height>160</Height>
  8.   </Person>
  9.   <Person>
  10.     <FirstName>Bugs</FirstName>
  11.     <LastName>Bunny</LastName>
  12.     <DateOfBirth>1999-02-07T00:00:00</DateOfBirth>
  13.     <Height>170</Height>
  14.   </Person>
  15.   <Person>
  16.     <FirstName>Peter</FirstName>
  17.     <LastName>Pan</LastName>
  18.     <DateOfBirth>2001-10-31T00:00:00</DateOfBirth>
  19.     <Height>150</Height>
  20.   </Person>
  21. </Persons>

One thing to note is that the xml declaration is automatically added. DateTime field – DateOfBirth is automatically converted to its string representation.

The xml produced previously contains elements only. The Linq query could be easily updated to change the data to be the attributes of xml elements. This is shown in another query – “query2”. System.Xml.Linq.XAttribute objects are created similarly instead of XElement objects and added to XElement object which is then added to XDocument object and saved as xml. The resultant xml file looks like below.

Xml from Linq to ArrayList
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Persons>
  3.   <Person FirstName="Scooby" LastName="Doo" DateOfBirth="2000-12-31T00:00:00" Height="160" />
  4.   <Person FirstName="Bugs" LastName="Bunny" DateOfBirth="1999-02-07T00:00:00" Height="170" />
  5.   <Person FirstName="Peter" LastName="Pan" DateOfBirth="2001-10-31T00:00:00" Height="150" />
  6. </Persons>

The data looks similar to the previous xml except that they are now attributes of “Person” element instead of child elements.

Reference: Shahed Kazi at AspNetify.com