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. XElement class
Class is defined as below in namespace System.Xml.Linq.
And it inherits the class XContainer
that derives from XNode
[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);
Constructor of XAttribute
- It is one of the base and fundamental class in LINQ to XML.
- It represents XML element.
- 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.
- This class can be use to serialize the content in a text form.
- This class can be used to create XML tree.
If you see above definition of XAttribute
class; there are five constructors. Usually we use the below constructor public XElement(XName name, object content);
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
. This root element is having many nested XElement,
- XElement Data1 is having one XAttribute. Name of the Attribute is "name" and value of attribute is "dj".
- 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("Data3", "3"),
new XElement("Data4", "4")
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.