SIGN UP MEMBER LOGIN:    
ARTICLE

Reading and Writing XML using XLinq

Posted by Mahesh Chand Articles | LINQ with C# June 21, 2007
XLinq is a new API to work with XML using LINQ. XLinq is more flexible, simple, and faster programming model to read, write, manipulate, and traverse XML documents.
Reader Level:

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" ?>

- <Authors>
- <Author Author="Mahesh Chand">
  <Book>GDI+ Programming</Book>
  <Cost>$49.95</Cost>
  <Publisher>Addison-Wesley</Publisher>
  </Author>
- <Author Name="Mike Gold">
  <Book>Programmer's Guide to C#</Book>
  <Cost>$44.95</Cost>
  <Publisher>Microgold Publishing</Publisher>
  </Author>
- <Author Name="Scott Lysle">
  <Book>Custom Controls</Book>
  <Cost>$39.95</Cost>
  <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);
            }

 

Login to add your contents and source code to this article
share this article :
post comment
 

Is possible create the follow code in typed code? Like typed dataset?

Posted by Rodrigo Alves Aug 12, 2011

Thanks David.

Cheers!

Posted by Mahesh Chand Jul 23, 2007

After reading your article I decided to investigate more about XLinq and I found a shorter and easier to read solution:

XElement authors =
new XElement("Authors",
new XElement("Author",
new XAttribute("Name", "Mahesh Chand"),
new XElement("Book", "GDI+ Programming"),
new XElement("Price", "$49.95"),
new XElement("Publisher", "Addison-Wisley")
),
new XElement("Author",
new XAttribute("Name", "Mike Gold"),
new XElement("Book", "Programmer's Guide to C#"),
new XElement("Price", "$44.95"),
new XElement("Publisher", "Microgold Publishing")
),
new XElement("Author",
new XAttribute("Name", "Scott Lysle"),
new XElement("Book", "Custom Controls"),
new XElement("Cost", "$39.95"),
new XElement("Publisher", "C# Corner")
)
);
authors.Save(@"Authors.xml");
Great article. Thanks! David.

Posted by david canar Jul 22, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor