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



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 :02/03/2010   Points :25   Category: LINQ    URL: http://www.dhananjaykumar.net

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.


Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
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