LINQ to XML Part #2 - XElement Class

No.of Views1054
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  17 Apr 2010 10:04:55
Tag : LINQ , Miscellaneous
In this article, I will give explanation on XElement class. This class is used to construct XML Elements.
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

In this article, I will give explanation on 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. 

 

Image Loading

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(XName name);
        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 class 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 XML tree.

Constructor of XAttribute
 

If you see above definition of XAttribute class; there are five constructors. Usually we use the below 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 below example; I am constructing a XElment called xmltree. This root element is having 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 XElement("Data2", new XAttribute("ID", "U18949"),
                                 new XAttribute("DEPT","MIT"),2),
                                 new XElement("Data3", "3"),
                                 new XElement("Data4", "4")
                                );
Console.WriteLine(xmltree);            

Samplem Output

Image Loading

Conclusion

In this article; I explained about XElement class. In next article, I will explain about CRUD operation on XML using LINQ to XML 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
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