SIGN UP MEMBER LOGIN:    
ARTICLE

LINQ to XML Part 2: (XElement Class)

Posted by Dhananjay Kumar Articles | LINQ with C# February 22, 2010
In this article I describe the XElement class. This class is used to construct XML Elements.
Reader Level:

Objective

In this article I describe the XElement class. This class is used to construct XML Elements.

What is Elements in XML?

XML Element is fundamental XML constructs. An Element has a name and optional attributes. An XML Elements can have nested Elements called Nodes also.

image1.gif

XElement class

XElement Class is defined as below in namespace System.Xml.Linq. And it inherits the class XContainer that derives from XNode .

namespace System.Xml.Linq
{
    [XmlSchemaProvider("", IsAny = true)]
    public class XElement : XContainer, IXmlSerializable
    {
        public XElement(XElement other);     

        public XElement(XStreamingElement other);       
        public XElement(XName name, object content);       
        public XElement(XName name, params object[] content);


5 Facts:

  1. It is one of the base and fundamental classes in LINQ to XML.
  2. It represents XML element.
  3. This class can be used to change the content of the element.
    a. It can add child element.
    b. It can delete child element.
    c. It can change child element.
    d. It can add attributes to an element.
  4. This class can be use to serialize the content in a text form.
  5. This class can be used to create an XML tree.

Constructor of XAttribute

If you see above definition of XAttribute class; there are five constructors. Usually we use the following constructor

public XElement(XName name, object content);       

name: It is unique name of Attribute in XML tree.

content: It is content of the attribute. It is of type object.

Example

In the following example, I am constructing a XElment called xmltree. This root element is has many nested XElement,

  1. XElement Data1 is having one XAttribute. Name of the Attribute is "name" and value of attribute is "dj".
  2. XElement Data2 is having two XAttribute. They are ID and DEPT with values U18949 and MIT respectively.

XElement xmltree = new XElement("Root",
                                 new XElement("Data1", new XAttribute("name", "Dj"), 1),

                                 new XAttribute("DEPT","MIT"),2),
                                 new XElement("Data3", "3"),
                                 new XElement("Data4", "4")
                                );
Console.WriteLine(xmltree);           

Output

image2.gif

Conclusion

In this article; I described the XElement class. In the next article, I will explain about CRUD operation on XML using LINQ to XML. Thanks for reading. 

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

This looks nice one, but i did not understand how the element data2 element is created

Posted by VISHVANATHA ACHARY Apr 08, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor