What is XLinq?
XLinq is the name for API to work with XML in next version of .NET with the power of LINQ. It provides functionality to read, write, and manipulate XML documents. XLinq lets you run SQL like queries on XML documents.
Why XLinq?
The answer to this question is, XLinq is redesigned from scratch to keep in mind the problems with existing technologies including Document Object Model (DOM), XmlReader, and XSLT, MSXML.
XLinq is more powerful, flexible, and a single API to provide all functionality to read, write, manipulate, and traverse XML document. It provides all functionalities of DOM, XQuery, XPath, XSLT, and MSXML.
I will discuss XLinq features in more details in my forthcoming articles.
This article shows how to create an XML document using XLinq and later load it and loop through the elements of XML.
Note: To run this sample, you must have Visual Studio 2005 and LINQ CTP May 2006 installed on your machine.
You must import following namespace:
using
System.Xml.XLinq;
The XElement class represents an XML element in XLinq. The code creaes a root node called Authors and adds children Author nodes. The XAttribute class represents an attribute of an element.
XElement.Save method saves the contents of XElement to a XML file.
// Create a root node
XElement authors = new XElement("Authors");
// Add child nodes
XAttribute name = new XAttribute("Author", "Mahesh Chand");
XElement book = new XElement("Book", "GDI+ Programming");
XElement cost = new XElement("Cost", "$49.95");
XElement publisher = new XElement("Publisher", "Addison-Wesley");
XElement author = new XElement("Author");
author.Add(name);
author.Add(book);
author.Add(cost);
author.Add(publisher);
authors.Add(author);
name = new XAttribute("Name", "Mike Gold");
book = new XElement("Book", "Programmer's Guide to C#");
cost = new XElement("Cost", "$44.95");
publisher = new XElement("Publisher", "Microgold Publishing");
author = new XElement("Author");
author.Add(name);
author.Add(book);
author.Add(cost);
author.Add(publisher);
authors.Add(author);
name = new XAttribute("Name", "Scott Lysle");
book = new XElement("Book", "Custom Controls");
cost = new XElement("Cost", "$39.95");
publisher = new XElement("Publisher", "C# Corner");
author = new XElement("Author");
author.Add(name);
author.Add(book);
author.Add(cost);
author.Add(publisher);
authors.Add(author);
authors.Save(@"Authors.xml");
The output xml file looks like this:
<?xml version="1.0" encoding="utf-8" ?>
- <Author Author="Mahesh Chand">
<Book>GDI+ Programming</Book>
<Publisher>Addison-Wesley</Publisher>
</Author>
- <Author Name="Mike Gold">
<Book>Programmer's Guide to C#</Book>
<Publisher>Microgold Publishing</Publisher>
</Author>
- <Author Name="Scott Lysle">
<Book>Custom Controls</Book>
<Publisher>C# Corner</Publisher>
</Author>
</Authors>
The following code reads the Authors.xml file and loops through authors and displays the contents.
XElement allData = XElement.Load("Authors.xml");
if (allData != null)
{
IEnumerable<XElement> authors = allData.Descendants("Author");
foreach(XElement author in authors)
Console.WriteLine((string)author);
}