Introduction Using LINQ in XML

Introduction Using LINQ in XML

By : Kasim Wirama, MCDBA, MVP SQL Server

This article, I would begin to explore LINQ implementation in XML. .NET framework 3.5 provide API dll for LINQ to XML through System.Xml.Linq, so include this into project reference, and also add it as namespace.

Most commonly used classes under System.Xml.Linq are XDocument, XDeclaration, XElement, and XAttributes.

These classes above has corresponding classes in DOM class, but classes under LINQ is much more simpler as long as you make your code formatting clear.

For example, assume there is XML structure as shown below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<contact id="001>

  <firstName>Johan</firstName>

  <lastName>Santoso</lastName>

  <addresses>

                <address type="home">

                                <addressItem ownership="rent">Sudirman St no. 2</addressItem>

                                <addressItem ownership="buy">Orchad Rd no. 3</addressItem>

                </address>

                <address type="apartment">

                                <addressItem ownership="buy">Thumb St 4</addressItem>

                </address>

  </addresses>

</contact>

I can write code to achieve the XML above as :

XDocument doc = new XDocument(

    new XDeclaration("1.0","utf-8","yes"),
        new XElement("contact", new XAttribute("id","001"),
            new XElement("firstname","Johan"),
            new XElement("lastname","Santoso"),
            new XElement("addresses",
                new XElement("address", new XAttribute("type","home"),
                    new XElement("addressItem",new XAttribute("ownership","rent"),"Sudirman St no.2"),
                    new XElement("addressItem", new XAttribute("ownership", "buy"), "Orchad Rd no. 3")
                    ),
                new XElement("address", new XAttribute("type","apartment"), 
                   new XElement("addressItem", new XAttribute("ownership", "buy"), "Thumb St 4")
                                                  )
                )
            )
    );

Try experimenting by yourself, and you will know how easy to generate XML structure with LINQ rather than using DOM. This single statement above is called functional construction.You can display the XML output to console window by calling ToString method on instace of XDocument.For querying with LINQ, I begin the most simple one as warming up. On next subsequent articles, I will go deeper about LINQ querying capability in XML. You can get list of addresses by getting descendants of Address element as code shown belowforeach(XElement child in doc.Descendants("address").Elements()){    Console.WriteLine(child);}It will return result :<addressItem ownership="rent">Sudirman St no. 2</addressItem>
<addressItem ownership="buy">Orchad Rd no. 3</addressItem>
<addressItem ownership="buy">Thumb St 4</addressItem>

You can try to set descendants to addresses, and see what results it will return.

Share this post: | | | |
Published Friday, November 30, 2007 12:44 PM by Kasim.Wirama
Filed under:

Comments

No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems