Functional Construction of XML Tree in LINQ-Part03

No.of Views660
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  13 Apr 2010 11:04:26
Tag : LINQ , General
how to create a XML tree using Functional Construction method of LINQ to XML.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will give an explanation on; how to create a XML tree using Functional Construction method of LINQ to XML.

What is Functional Construction?

Functional Construction is ability to create a XML tree in a single statement.  LINQ to XML is being used to create XML tree in a single statement.
The features of LINQ to XML enables functional construction are as follows
1.    The XElement class constructor takes various types of arguments
a.    For child element takes another XElement as argument.
b.    For attribute of element takes XAttribute as argument.
c.    For text content of element takes simple string as argument.
2.    For complex type of content pass parameter as Array of Objects.
3.    If an object implements IEnumerable(T) , then the collection is enumerated. If the collection contains XElement or XAttributes objects then result of LINQ query can be passing as parameter to XElement constructor.
 

Sample #1

Here we are creating a XML tree by passing XElement as child element and XAttribute as attribute to one of the element. 

XElement xmltree = new XElement("Root",
                               new XElement("Element1", new XAttribute("name", "Dj"), 1),
                               new XElement("Element2", new XAttribute("ID","U18949"),
                               new XAttribute("DEPT","MIT"),2),
                               new XElement("Element3", "3"),
                               new XElement("Element4", "4")
                                );
Console.WriteLine(xmltree);

Please do not forget to add System.XML.LINQ namespace. Output in a console print may look like

 

Image Loading

Sample # 2

Now we will try to use feature #3 (Discussed above) that if object implements IEnumerable(T) we can pass result of a LINQ query as parameter . 
So let us say, that in above XML Tree (Created as sample1), we are retrieving child elements with content 3 and 4  and passing the result as parameter of constructor of XML element  to create XML tree.
 

XElement xmltree = new XElement("Root",
                  new XElement("Element1", new XAttribute("name", "Dj"), 1),
                  new XElement("Element2", new XAttribute("ID", "U18949"),
                  new XAttribute("DEPT","MIT"),2),
                  new XElement("Element3", "3"),
                  new XElement("Element4", "4")
                    );
Console.WriteLine(xmltree);

XElement newXmlTree = new XElement ("ROOT",
                                     new XElement("Element1",1),
                                     new XElement("Element2",2),
                                   from e in xmltree.Elements() where (int) e >2 select e
                                   );
Console.WriteLine("New XML Tree using LINQ query ");
Console.WriteLine(newXmlTree);

If you see the above code, the second XML tree, we are passing LINQ query result as parameter of XElement. We will get expected output as below.

 

Image Loading

Conclusion

In this article, I explained about FUNCTIONAL CONSTRUCTION way of creating XML TREE. Thanks for reading.
 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
    Deferred Execution executes the query only when Loop starts. What I mean here is that, we iterate through the query variable to get the result.
    Published Date : 15/Jan/2011
    In this article we will focus LINQ Remote and Local exaction such as This query executes on the server, Remote execution of query is default in LINQ, In Remote execution advantage of Databases index can be taken, Remote execution allows us to take the advantage of Database server engine, Remote execution allows us to only select particular rows from the table. This is very useful when we do have large amount of data in the server.
    Published Date : 07/Jan/2011
    In this article, we will see how to work with LINQ against IIS.
    Published Date : 10/Aug/2010
    This article demonstrates how to use LINQ on DataTable, XML Data using LINQ to XML and SQL server data base using LINQ to SQL Classes.
    Published Date : 28/Jun/2010
    In this article we will focus, how to read CSV file using LINQ to CSVProvider.I hope we know read CSV file using StreamReader or any other readers in C#. But when we are use the LINQ to CSVProvider, and then we could get additional features to query data in LINQ as like Sql Query.
    Published Date : 01/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top