LINQ to XML

The .NET Framework provided us with several techniques for accessing and  manipulating an XML file. In the past, you can use the XmlReader or the different XML Document Object Model classes such as XmlDocumentXmlElement,  and XmlComment. You can also use XML querying  languages such as XPath or XQuery to select specific elements in an XML  document. The problem is, you have to know this language which really has  nothing to do with C#. That’s where LINQ to XML comes in. With LINQ to XML, you  can use the LINQ operators to query, manipulate, and create XML documents. If  you want to learn the old and non-LINQ way of manipulating and creating XML  documents, this site already offers some tutorials for those.

Let’s take a look at a simple example of using LINQ to XML on a preexisting XML  file containing some data.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 
<Persons>
  <Person name="John Smith">
    <Age>30</Age>
    <Gender>Male</Gender>
  </Person>
  <Person name="Mike Folley">
    <Age>25</Age>
    <Gender>Male</Gender>
  </Person>
  <Person name="Lisa Carter">
    <Age>22</Age>
    <Gender>Female</Gender>
  </Person>
  <Person name="Jerry Frost">
    <Age>27</Age>
    <Gender>Male</Gender>
  </Person>
  <Person name="Adam Wong">
    <Age>35</Age>
    <Gender>Male</Gender>
  </Person>
</Persons>

Example 1 – A Sample XML File

Open Visual Studio and create a new Console Application project and name it  LinqToXmlPractice. Right click the solution in the Solution Explorer and add a  new item. Choose XML file from the list of template and name it sample.xml.  Replace the contents of that file with the one from Example 1.

Our sample.xml file contains the XML markup  that we will try to query. If you are unfamiliar with XML, there are many good  tutorials on the internet that will teach you its concepts. There is also a quick introduction to XML that can be found in this site.

The XML file contains one root element named Persons. It contains multiple  child elements named Person. Each child element has a name attribute and  contains yet another set of child elements, the Age and Gender elements. Now  let’s write some LINQ query to retrieve, let’s say, the names of every person in  the XML Document. Open up Program.cs and add using statement for the System.Linq.Xml namespace.

using System.Linq.Xml;

Inside the Main method, write the  following:

XDocument doc = XDocument.Load(@"..\..\sample.xml");

var names = from d in doc.Root.Elements("Person")
            select d.Attribute("name").Value;

Console.WriteLine("Names of every person from the XML file.");

foreach (var name in names)
{
    Console.WriteLine("{0}", name);
}

Example 2 – Using LINQ to query XML elements

Names of every person from the XML file.
John Smith
Mike Folley
Lisa Carter
Jerry Frost
Adam Wong

The full explanation for the codes will be in the upcoming lessons. But for now,  you can see how easy it is to get any data you want from the elements of an XML  document.