XElement Class

Namespace: System.Xml.Linq
Assembly: System.Xml.Linq.dll

Represents an XML element with optional attributes, child elements, and annotation. This class is part of the Language Integrated Query (LINQ) to XML API.

Syntax

public class XElement : XContainer, IEquatable<XElement>

Inheritance

  • System.Object
  • System.Xml.Linq.XObject
  • System.Xml.Linq.XContainer
  • System.Xml.Linq.XElement

Remarks

The XElement class provides a powerful and convenient way to create, query, and manipulate XML data in memory. It integrates seamlessly with LINQ, allowing you to use standard LINQ query operators to retrieve information from your XML structure.

An XElement object represents an XML element, which can have the following components:

  • A name (XName)
  • Attributes (IEnumerable<XAttribute>)
  • Child nodes, which can include other XElement objects, XAttribute objects, XText objects, XComment objects, XCData objects, and XProcessingInstruction objects.
  • Annotations (IEnumerable<object>)

You can construct XElement objects using object initializers, which is a concise and readable way to define XML structures. For example:

var element = new XElement("Person",
                                new XAttribute("Id", 1),
                                new XElement("Name", "John Doe"),
                                new XElement("Age", 30)
                            );

Constructors

Name Description
XElement(XName name) Initializes a new instance of the XElement class with the specified name.
XElement(XName name, object content) Initializes a new instance of the XElement class with the specified name and content.
XElement(XName name, params object[] content) Initializes a new instance of the XElement class with the specified name and an array of content.
XElement(XElement other) Initializes a new instance of the XElement class from another XElement.
XElement(XmlReader reader) Initializes a new instance of the XElement class from an XmlReader.

Properties

Name Description
Name Gets the qualified name of the element.
Value Gets or sets the value of this element or first child element.
FirstAttribute Gets the first attribute of this element.
LastAttribute Gets the last attribute of this element.
FirstNode Gets the first child node of this element.
LastNode Gets the last child node of this element.
NodeType Gets the node type of this node.
Parent Gets the parent node of this node.
Document Gets the document that contains this node.
NextNode Gets the next sibling node.
PreviousNode Gets the previous sibling node.

Methods

Name Description
Add(object content) Adds the specified content to the end of the child collection of this element.
Add(params object[] content) Adds the specified content to the end of the child collection of this element.
AddFirst(object content) Adds the specified content to the beginning of the child collection of this element.
Attribute(XName name) Gets the attribute with the specified name.
Element(XName name) Gets the first child element with the specified name.
Elements() Returns a collection of the child elements of this element.
Elements(XName name) Returns a collection of the child elements with the specified name.
Nodes() Returns a collection of the child nodes of this element.
RemoveAttributes() Removes all attributes from this element.
RemoveNodes() Removes all child nodes from this element.
SetAttributeValue(XName name, object value) Sets the value of the attribute with the specified name. If the attribute does not exist, it is created.
SetElementValue(XName name, object value) Sets the value of the first child element with the specified name. If the element does not exist, it is created.
Load(string path) Loads an XDocument from the specified file path.
Load(Stream stream) Loads an XDocument from the specified stream.
Parse(string text) Parses an XML string and returns an XDocument.