Skip to content
September 23, 2008 / kiranpatils

Generic List to XML using LINQ

Let us see  how to generate list of XML using LINQ.
 
Scenario
I have one generic List of Employee and want to convert it in to XML format.
 
Solution:
I can do this using XmlDocument class which is .NET 2.0 but it can take more than 30-40 lines..and i want to achieve[achieved also!!]
it in 4-5 Lines…sounds cool idea..so lets see how can we achieve it using LINQ–Language integrated Query new poweful tool of Micro.
 
First i have this Simple Class for storing Employee value:
 
Employee.CS
——————-
using System;
namespace LinqtoXMLDemo
{
 public class Employee
 {
  public int EmployeeID
  {
   get;
   set;
  }
  public string EmployeeName
  {
   get;
   set;
  }
  public string EmployeeAddress
  {
   get;
   set;
  }
 }
}
 
Ok Lets start
 
1. Add some sample values in list
List<Employee> employeeList = new List<Employee>();
      
   employeeList.Add(new Employee()
   {
    EmployeeAddress=”BARODA”,
    EmployeeID=1,
    EmployeeName=”XYZ”    
   });
   employeeList.Add(new Employee()
   {
    EmployeeAddress = “Bangalore”,
    EmployeeID = 2,
    EmployeeName = “ABC”
   });
 
2. Now i want to convert it into XML format..which should look like this..
<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>
<Employees>
 <Employee>
  <EmployeeID>1</EmployeeID>
  <EmployeeName>XYZ</EmployeeName>
  <EmployeeAddress>BARODA</EmployeeAddress>
 </Employee>
 <Employee>
  <EmployeeID>2</EmployeeID>
  <EmployeeName>ABC</EmployeeName>
  <EmployeeAddress>Bangalore</EmployeeAddress>
 </Employee>
</Employees>
 
3. Method to achieve our desire..[Which is self explanatory)
/// <summary>
  /// This Method will generate list TO XML using LINQ
  /// </summary>
  /// <param name=”employeeList”>Collection of Employe</param>
  /// <returns>XmlDocument</returns>
  private static XDocument GenerateListToXML(List<Employee> employeeList)
  {
   XDocument xmlDocument = new XDocument(new XDeclaration(“1.0”, “UTF-8”, “yes”),
              new XElement(“Employees”,
              from employee in employeeList
              select new XElement(“Employee”,
              new XElement(“EmployeeID”, employee.EmployeeID),
                  new XElement(“EmployeeName”, employee.EmployeeName),
                   new XElement(“EmployeeAddress”, employee.EmployeeAddress))));
   return xmlDocument;
  }
 
4. From main call it like this:
 
XDocument employeeXML = GenerateListToXML(employeeList);
That’s it we are ready!!!!
Okay XML Looks cool..But now i want it with namespace so it should look like this..
<?xml version=”1.0″ encoding=”utf-8″ standalone=”yes”?>
<Employees xmlns=”
http://www.abc.org/employees/0.9“>
 <Employee>
  <EmployeeID>1</EmployeeID>
  <EmployeeName>XYZ</EmployeeName>
  <EmployeeAddress>BARODA</EmployeeAddress>
 </Employee>
 <Employee>
  <EmployeeID>2</EmployeeID>
  <EmployeeName>ABC</EmployeeName>
  <EmployeeAddress>Bangalore</EmployeeAddress>
 </Employee>
</Employees>
Method to generate XML with Namespace
/// <summary>
  /// This Method will generate list TO XML with namespace using LINQ
  /// </summary>
  /// <param name=”employeeList”>Collection of Employe</param>
  /// <returns>XmlDocument</returns>
  private static XDocument GenerateListToXMLWithNameSpace(List<Employee> employeeList)
  {
   XNamespace employeeNameSpace = “
http://www.abc.org/employees/0.9“;
   XDocument xmlDocument = new XDocument(new XDeclaration(“1.0”, “UTF-8”, “yes”),
              new XElement(employeeNameSpace + “Employees”,
              from employee in employeeList
              select new XElement(employeeNameSpace + “Employee”,
              new XElement(employeeNameSpace + “EmployeeID”, employee.EmployeeID),
                  new XElement(employeeNameSpace + “EmployeeName”, employee.EmployeeName),
                   new XElement(employeeNameSpace + “EmployeeAddress”, employee.EmployeeAddress))));
   return xmlDocument;
  }
 
That’s it…Happy LINQ!!
 
 

One Comment

Leave a Comment
  1. madhu / Mar 8 2011 9:06 pm

    how can i do the same if i have multiple lists which need to be written in a single xml?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: