Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
LINQ to XML : Creating complex XML through LINQ

We can generate hierarchical object graph in our memory though LINQ. To be more realistic we can bring data from relational database. So if we consider Northwind database and use LINQ to SQL to bring all the Customers and their Orders and Order Details the query would look like,

 

 

//LINQ to SQL way to get data from database

var q = from c in db.Customers

        select new

        {

            CId = c.CustomerID,

            Orders = from o in c.Orders

                     select new

                     {

                         OID = o.OrderID,

                         Qty = from od in o.Order_Details

                               select new { Qty = od.Quantity }

                     }

        };

 

 

 

So what I am trying to do here is that, I am trying to fetch CustomerId from Customers table and OrderId from Orders table and Quantity from Order Details table. It is bringing 3 level deep data for me and storing it to memory.

 

By using XElement and XAttribute I will create a single XML stream. Which will look like,

 

<?xml version="1.0" encoding="utf-8"?>

<customers>

  <customer id="ALFKI" country="Germany" contactName="Maria Anders" contactTitle="Sales Representative">

    <Orders id="10643" date="1997-08-25T00:00:00">

      <items>

        <item price="45.6000" quantity="15" />

        <item price="18.0000" quantity="21" />

        <item price="12.0000" quantity="2" />

      </items>

    </Orders>

    <Orders id="10692" date="1997-10-03T00:00:00">

      <items>

        <item price="43.9000" quantity="20" />

      </items>

    </Orders>

 

…….

 

To achieve this I have to write a very simple query like syntax based on the query I have written earlier,

 

var query = new XElement("customers",

           from c in db.Customers                       

           select

               new XElement("customer",

                   new XAttribute("id", c.CustomerID),

                   new XAttribute("country", c.Country),

                   new XAttribute("contactName", c.ContactName),

                   new XAttribute("contactTitle", c.ContactTitle),

 

                   from o in c.Orders

                   select new XElement("Orders",

                       new XAttribute("id", o.OrderID),

                       new XAttribute("date", o.OrderDate),

                            new XElement("items",

 

                       from od in o.Order_Details

                       select new XElement("item",

                               new XAttribute("price", od.UnitPrice),

                               new XAttribute("quantity", od.Quantity))))));

 

 

It looks complex because it is one liner but actually it is very simple. This will give you the exact XML output mentioned earlier.

 

Namoskar!!!

Posted: Monday, February 18, 2008 7:41 PM by wriju

Comments

Noticias externas said:

We can generate hierarchical object graph in our memory though LINQ. To be more realistic we can bring

# February 18, 2008 2:46 PM

Wriju's BLOG said:

Based on my previous post if I have to filter the list of customers only in the country “US” and create

# February 18, 2008 3:17 PM

Charlie Calvert's Community Blog said:

Welcome to the forty-first Community Convergence. The big news this week is that we have moved Future

# April 19, 2008 4:12 AM

Tim Stewart said:

How did you get the <?xml ...?> processing instruction to be part of the XML you generated.

# July 9, 2008 2:52 PM

wriju said:

@Tim,

When you call .Save() method it automatically gets added. Else you could also use XDocument and then initialize XProcessingIn.... to add you own. For details please visit my

http://blogs.msdn.com/wriju/archive/2008/02/28/linq-to-xml-creating-complete-xml-document.aspx

# July 10, 2008 9:27 AM

Ezequiel said:

there is no way to do this:

 Dim contacts as XElement =  _

   <contacts>

     <%= From c In contacts  _

         Select _

           <>

             <!-- contact -->

             <name><%= c.<name>.Value %> </name>

             <%= c.<phone> %>

             <address><%= c.<address> %> </address>

     </>

     %>

   </contacts>

in C#??

# February 18, 2009 3:50 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker