Using Linq we can create the xml document very easily. This document explains as to how we can create the xml document using Linq.
Let’s create a simple xml document which has customer details and order details for each customer. For this we need to have the customer’s collection which contains the required data to create the xml. Create the required customer class and the order class as shown below.
public class Customers {
public int Id { get; set; }
public string Name {get; set;}
public string Phone { get; set; }
public string Email { get; set; }
public List<Orders> Orders {get; set;}
}
public class Orders {
public string OrderName { get; set; }
public int Quantity { get; set; }
public string Date { get; set; }
}
Then we create the Customers collection as shown below.
List<Customers> customers = new List<Customers>() {
New Customers(){Id=1,Name=”Customer1″,Phone=”9845884650″, Email=”Customer1@gmail.com”,
Orders=new List<Orders>() {
New Orders(){ Date=DateTime.Today.ToShortDateString(), OrderName=”OrderName1″, Quantity=2},
New Orders(){Date=DateTime.Today.AddDays(1).ToShortDateString(), OrderName=”OrderName2″, Quantity=3},
new Orders(){Date=DateTime.Today.AddDays(2).ToShortDateString(), OrderName=”OrderName3″, Quantity=4},
}},
new Customers(){Id=2,Name=”Customer2″,Phone=”9845884651″, Email=”Customer2@gmail.com”,
Orders=new List<Orders>() {
new Orders(){Date=DateTime.Today.AddDays(3).ToShortDateString(), OrderName=”OrderName1″, Quantity=5},
new Orders(){Date=DateTime.Today.AddDays(4).ToShortDateString(), OrderName=”OrderName2″, Quantity=6},
new Orders(){Date=DateTime.Today.AddDays(5).ToShortDateString(), OrderName=”OrderName3″, Quantity=7},
}},
new Customers(){Id=3,Name=”Customer3″,Phone=”9845884652″, Email=”Customer3@gmail.com”,
Orders=new List<Orders>() {
new Orders(){Date=DateTime.Today.AddDays(6).ToShortDateString(), OrderName=”OrderName1″, Quantity=8},
new Orders(){Date=DateTime.Today.AddDays(7).ToShortDateString(), OrderName=”OrderName2″, Quantity=9},
new Orders(){Date=DateTime.Today.AddDays(8).ToShortDateString(), OrderName=”OrderName3″, Quantity=10},
}}
};
Once we have the customers collection, then to create the xml document we create the root XElement first and then query the customers collection and foreach customer collection we add a new Child xelement as shown below.
// The root element
XElement xelement = new XElement(“Customers”,
from customer in customers
// Adding each customer info as the child element
select new XElement(“Customer”,
new XAttribute(“ID”, customer.Id),
new XAttribute(“Email” ,customer.Email),
new XElement(“Name”, customer.Name),
new XElement(“Phone”, customer.Phone),
// Each customer can have more then one orders, so querying the orders collection and adding the exisitng order details
from order in customer.Orders
select new XElement(“Order”,
new XAttribute(“OrderDate”, order.Date),
new XElement(“OrderName”, order.OrderName),
new XElement(“OrderQuantity”, order.Quantity))));
The generate xml document wil be as shown below
<Customers>
<Customer Email=”Customer1@gmail.com”>
<Name>Customer1</Name>
<Phone>9845884650</Phone>
<Order OrderDate=”8/2/2010″>
<OrderName>OrderName1</OrderName>
<OrderQuantity>2</OrderQuantity>
</Order>
<Order OrderDate=”8/3/2010″>
<OrderName>OrderName2</OrderName>
<OrderQuantity>3</OrderQuantity>
</Order>
<Order OrderDate=”8/4/2010″>
<OrderName>OrderName3</OrderName>
<OrderQuantity>4</OrderQuantity>
</Order>
</Customer>
<Customer Email=”Customer2@gmail.com”>
<Name>Customer2</Name>
<Phone>9845884651</Phone>
<Order OrderDate=”8/5/2010″>
<OrderName>OrderName1</OrderName>
<OrderQuantity>5</OrderQuantity>
</Order>
<Order OrderDate=”8/6/2010″>
<OrderName>OrderName2</OrderName>
<OrderQuantity>6</OrderQuantity>
</Order>
<Order OrderDate=”8/7/2010″>
<OrderName>OrderName3</OrderName>
<OrderQuantity>7</OrderQuantity>
</Order>
</Customer>
<Customer Email=”Customer3@gmail.com”>
<Name>Customer3</Name>
<Phone>9845884652</Phone>
<Order OrderDate=”8/8/2010″>
<OrderName>OrderName1</OrderName>
<OrderQuantity>8</OrderQuantity>
</Order>
<Order OrderDate=”8/9/2010″>
<OrderName>OrderName2</OrderName>
<OrderQuantity>9</OrderQuantity>
</Order>
<Order OrderDate=”8/10/2010″>
<OrderName>OrderName3</OrderName>
<OrderQuantity>10</OrderQuantity>
</Order>
</Customer>
</Customers>








