How to generate data in XML format using LINQ

To create XML using LINQ as follows

Step 1: XElement class takes care of converting the LINQ query resultant into data in XML format. So, declare

the name space “using System.Xml.Linq;”

Step 2:
Now, the LINQ query should be written in the below format:

DataClassesDataContext dlDC = new DataClassesDataContext();
var inventory = new XElement("InventoryINFO", from oinventory in dlDC.Inventories
where oinventory.DealerID == "f91d585d4b1e" && oinventory.Deleted == "false"
select new
XElement("Inventory",
new XElement("ID", oinventory.InventoryID),
new XElement("NAME", oinventory.FirstName + " " + oinventory.LastName 
new XElement("TYPE", oinventory.BodyType)
)
);

 Step 3: The ‘inventory’ object stores all the data, Now i want to display the data in XML format, using
Response.Write as follows.

Response.Clear();
Response.ContentType = "text/xml";
Response.Write(inventory.ToString());
Response.End();

 
The output will look like:

 
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
MindFire solutions
car 


 
yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
Test
Bike 

..............................

150 150 Burnignorance | Where Minds Meet And Sparks Fly!