Introduction to LINQ, Part 2: LINQ to XML

In the first article of the series, I introduced the LINQ to Objects API, showing how the language integrated queries can be used for querying sequences with C#. In this second article, I will introduce LINQ to XML, formerly code-named XLinq.

The System.Xml.Linq Namespace

LINQ to XML allows querying XML data. A new namespace in .NET 3.5, System.Xml.Linq, contains classes for creating and querying XML trees, in the XPath style:

  • XName, used for namespace qualified identifies (Qname), used both as element and attribute name; XName objects are atomized, which means that if two XName objects have the same namespace and local name, they will actually share the same instance. This enables faster queries because, when filtering based on the name of elements or attributes, the comparison uses identity comparison, not value comparison. Checking if two references actually refer to the same object is much faster than comparing two strings.
  • XNode represents the abstract concept of a node in the XML tree. Acts as a base class for XComment, XContainer, XDocumentType, XProcessingInstruction, and XText.
  • XContainer, derived from XNode, offers features such as enumerating children of a node, or finding next and previous sibling; is the base class for XDocument and XElement.
  • XDocument represents an XML document, that contains the root level XML constructs, such as: a document type declaration (XDocumentType), one root element (XElement), zero or more comments objects (XComment).
  • XElement is the fundamental type for constructing XML data; it has an XName, optionally one or more attributes, and can also have content. An element is actually a container (derived from XContainer), that contains other XML nodes (XNode), such as XComment, XProcessingInstruction or XText.
  • XAttribute represents an XML attribute; in other words, a key-value pair associated with an XML element. Attributes are not nodes in the XML tree, thus not derived from XNode.
  • XComment, used for comments to the root node or as children of an element.

The following table shows how you can create different XML trees.

XElement root =
   new XElement("winner");
<winner />
XElement root =
   new XElement("winner",
      new XAttribute("year", 1999));
<winner year="1999" />
XElement root =
   new XElement("winner",
      new XAttribute("year", 1999),
      new XAttribute("country",
                     "England"));
<winner year="1999"
   country="England" />
XElement root =
   new XElement("winner",
      new XAttribute("year", 1999),
      new XAttribute("country",
                     "England"),
   "Manchester United");
<winner year="1999"
   country="England">
      Manchester United
</winner>
XElement root =
   new XElement("winner",
      new XAttribute("year", 1999),
      new XAttribute("country",
                     "England"),
      "Manchester United",
      new XComment("best final
                    ever"));
<winner year="1999"
   country="England">
      Manchester United
   <!-best final ever-->
</winner>
XElement root =
   new XElement("winners",
      new XElement("winner",
      new XElement("name",
         "Manchester United"),
      new XElement("country",
                   "England"),
      new XElement("year", 1999)));
<winners>
   <winner>
      <name>
         Manchester United
      </name>
      <country>
         England
      </country>
      <year>1999</year>
   </winner>
</winners>

Below is a hard-coded creation of an XML tree containing all the UEFA Champions League winners.

XElement root = new XElement("winners",
                    new XElement("winner",
                        new XElement("Name", "Barcelona"),
                        new XElement("Country", "Spania"),
                        new XElement("Year", 2006)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Liverpool"),
                        new XElement("Country", "Anglia"),
                        new XElement("Year", 2005)
                    ),
                    new XElement("winner",
                        new XElement("Name", "FC Porto"),
                        new XElement("Country", "Portugalia"),
                        new XElement("Year", 2004)
                    ),
                    new XElement("winner",
                        new XElement("Name", "AC Milan"),
                        new XElement("Country", "Italia"),
                        new XElement("Year", 2003)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Real Madrid"),
                        new XElement("Country", "Spania"),
                        new XElement("Year", 2002)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Bayern Munchen"),
                        new XElement("Country", "Germania"),
                        new XElement("Year", 2001)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Real Madrid"),
                        new XElement("Country", "Spania"),
                        new XElement("Year", 2000)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Manchester Utd."),
                        new XElement("Country", "Andlia"),
                        new XElement("Year", 1999)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Real Madrid"),
                        new XElement("Country", "Spania"),
                        new XElement("Year", 1998)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Borussia Dortmund"),
                        new XElement("Country", "Germania"),
                        new XElement("Year", 1997)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Juventus"),
                        new XElement("Country", "Italia"),
                        new XElement("Year", 1996)
                    ),
                    new XElement("winner",
                        new XElement("Name", "AFC Ajax"),
                        new XElement("Country", "Olanda"),
                        new XElement("Year", 1995)
                    ),
                    new XElement("winner",
                        new XElement("Name", "AC Milan"),
                        new XElement("Country", "Italia"),
                        new XElement("Year", 1994)
                    ),
                    new XElement("winner",
                        new XElement("Name", "Olympique de Marseille"),
                        new XElement("Country", "Franta"),
                        new XElement("Year", 1993)
                    )
                );

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read