Introduction
This article is the next in the LINQ series to make the learning easy for beginners to get a clear understanding of the concepts. I have seen young developers and students that are very confused about LINQ and XML. This article will help them to understand and leverage their knowledge and to learn the advanced features later.
If you are new to LINQ then please have a look at the basic articles for LINQ.
Background
LINQ to XML provides an in-memory XML programming interface that leverages the .NET Language-Integrated Query (LINQ) Framework.
MSDN Says, LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the .NET Framework programming languages.
It provides both DOM and XQuery/XPath like functionality in a consistent programming experience across the various LINQ-enabled data access technologies.
Functional construction
It is a way to create a XML tree using a single statement rather going for the traditional approach of DOM implementation.
There are certain classes that are used to create an XML tree with functional construction.
The following is LINQ to XML class hierarchy, I have copied this image from the MSDN to make the structure more clear.

The preceding diagram is from the MSDN.
There are many more classes that can be used in LINQ to XML. The following are a few of them to explain the functional construction of XML.
- XDocument
- XDeclaration
- XComment
- XElement
- XAttribute
CustomersDetail.xml
<?xml version="1.0" encoding="utf-8" standalone="yes"?> (XDeclaration)
<!--LINQ to XML Demo--> (XComment)
<Customers> (XElement)
<Customer CustID="1001">(CustID = XAttribute)
<Name>Abhishek</Name>
<MobileNo>9820098200</MobileNo>
<Location>Mumbai</Location>
<Address>off link road Malad west Mumbai</Address>
</Customer>
<Customer CustID="1002">
<Name>Rajesh</Name>
<MobileNo>9820011234</MobileNo>
<Location>New Delhi</Location>
<Address>off link road laljatnagar New Delhi</Address>
</Customer>
<Customer CustID="1003">
<Name>Rohan</Name>
<MobileNo>9820022200</MobileNo>
<Location>Mumbai</Location>
<Address> link road Kandivali west Mumbai</Address>
</Customer>
<Customer CustID="1004">
<Name>Sonali</Name>
<MobileNo>9820098200</MobileNo>
<Location>Mumbai</Location>
<Address>khar west Mumbai</Address>
</Customer>
</Customers>
Sample Code
This is sample code for the functional construction of XML using LINQ to XML with a statement. I have hard coded the values here just to explain the structure and classes used.
namespace LINQ_Learning
{
class Program
{
static void Main(string[] args)
{
XDocument xmlDocument = new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XComment("LINQ To XML Demo"),
new XElement("Customers",
new XElement("Customer",new XAttribute("CustID",1001),
new XElement("Name","Abhishek"),
new XElement("MobileNo","9820098200"),
new XElement("Location","Mumbai"),
new XElement("Address","off link road malad west Mumbai")),
new XElement("Customer",new XAttribute("CustID",1002),
new XElement("Name","Rajesh"),
new XElement("MobileNo","9820011234"),
new XElement("Location","New Delhi"),
new XElement("Address","off link road laljatnagar New delhi")),
new XElement("Customer",new XAttribute("CustID",1003),
new XElement("Name","Rohan"),
new XElement("MobileNo","9820022200"),
new XElement("Location","Mumbai"),
new XElement("Address"," link road Kandivali west Mumbai")),
new XElement("Customer",new XAttribute("CustID",1004),
new XElement("Name","Sonali"),
new XElement("MobileNo","9820098200"),
new XElement("Location","Mumbai"),
new XElement("Address","khar west Mumbai"))
));
xmlDocument.Save(@"C:\Dev\LINQ_Learning\CustomersDetail.xml");
Console.ReadLine();
}
}
}
The preceding example has hard-coded values just to explain how the functional construction works.
We can do that using in-memory objects or data from a database using ADO.Net or using the Entity Framework depending on your requirements.
Here I am explaining this using an in-memory object List.
Sample code
namespace LINQ_Learning
{
public class Customers
{
public int CustID { get; set; }
public string Name { get; set; }
public long MobileNo { get; set; }
public string Location { get; set; }
public string Address { get; set; }
public static List<Customers> GetCostomersDetail()
{
List<Customers> lstcustomer = new List<Customers>()
{
new Customers{CustID=10001,Name="Abhishek" , MobileNo=9820098200, Location="Mumbai",Address="off link road malad west Mumbai"},
new Customers{CustID=10001,Name="Rajesh" , MobileNo=9820011234, Location="New Delhi",Address="off link road laljatnagar New delhi"},
new Customers{CustID=10001,Name="Rohan" , MobileNo=9820011266, Location="Mumbai",Address="link road Kandivali west Mumbai"},
new Customers{CustID=10001,Name="Abhishek" , MobileNo=890012452, Location="Mumbai",Address="khar west Mumbai"}
};
return lstcustomer;
}
}
}
Main Method





Muhammad Aqib ShehzadPosted Jan 6, 2016, 4:56 AM
nice approach and ifnormative
Gowtham RajamanickamPosted Apr 11, 2015, 1:19 PM
great
Tom MohanPosted Mar 16, 2015, 12:06 PM
Great work
NitinPosted Mar 16, 2015, 2:43 AM
nice
Rahul Kumar SaxenaPosted Mar 16, 2015, 2:10 AM
Good Work..