.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Functional Construction of XML Tree : LINQ to XML Part 3

Posted By:Dhananjay Kumar       Posted Date: March 02, 2010    Points: 25    Category: LINQ    URL: http://www.dotnetspark.com  

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

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 
    1. For child element takes another XElement as argument. 
    2. For attribute of element takes XAttribute as argument.
    3. 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 

1.gif

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.

2.gif

Conclusion

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

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend