X​Element Class

Definition

Represents an XML element. See XElement Class Overview and the Remarks section on this page for usage information and examples.

public class XElement : System.Xml.Linq.XContainer, System.Xml.Serialization.IXmlSerializable
Inheritance
Implements

Inherited Members

System.Object

System.Xml.Linq.XContainer

System.Xml.Linq.XNode

System.Xml.Linq.XObject

Examples

The following example creates an XML tree. The content of the new element comes from a LINQ query.


                XElement xmlTree1 = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5),  
    new XElement("Child6", 6)  
);  

XElement xmlTree2 = new XElement("Root",  
    from el in xmlTree1.Elements()  
    where((int)el >= 3 && (int)el <= 5)  
    select el  
);  
Console.WriteLine(xmlTree2);  

                Dim xmlTree1 As XElement = _  
        <Root>  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
            <Child3>3</Child3>  
            <Child4>4</Child4>  
            <Child5>5</Child5>  
            <Child6>6</Child6>  
        </Root>  

Dim xmlTree2 As XElement = _   
    <Root>  
        <%= From el In xmlTree1.Elements() _  
            Where el.Value >= 3 And el.Value <= 5 _  
            Select el %>  
    </Root>  

Console.WriteLine(xmlTree2)  

This example produces the following output:


                <Root>  
  <Child3>3</Child3>  
  <Child4>4</Child4>  
  <Child5>5</Child5>  
</Root>  

The following is the same example, but in this case the XML is in a namespace. For more information, see Working with XML Namespaces.


                XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree1 = new XElement(aw + "Root",  
    new XElement(aw + "Child1", 1),  
    new XElement(aw + "Child2", 2),  
    new XElement(aw + "Child3", 3),  
    new XElement(aw + "Child4", 4),  
    new XElement(aw + "Child5", 5),  
    new XElement(aw + "Child6", 6)  
);  

XElement xmlTree2 = new XElement(aw + "Root",  
    from el in xmlTree1.Elements()  
    where((int)el >= 3 && (int)el <= 5)  
    select el  
);  
Console.WriteLine(xmlTree2);  

                Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree1 As XElement = _  
            <Root>  
                <Child1>1</Child1>  
                <Child2>2</Child2>  
                <Child3>3</Child3>  
                <Child4>4</Child4>  
                <Child5>5</Child5>  
                <Child6>6</Child6>  
            </Root>  

        Dim xmlTree2 As XElement = _   
            <Root>  
                <%= From el In xmlTree1.Elements() _  
                    Where el.Value >= 3 And el.Value <= 5 _  
                    Select el %>  
            </Root>  

        Console.WriteLine(xmlTree2)  
    End SUb  
End Module  

This example produces the following output:


                <Root xmlns="http://www.adventure-works.com">  
  <Child3>3</Child3>  
  <Child4>4</Child4>  
  <Child5>5</Child5>  
</Root>  

Remarks

Note

To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.

This class represents an XML element, the fundamental XML construct. See XElement Class Overview for other usage information.

An element has an XName, optionally one or more attributes, and can optionally contain content (for more information, see Nodes).

An XElement can contain the following types of content:

For details about the valid content of an XElement, see Valid Content of XElement and XDocument Objects.

XElement derives from XContainer, which derives from XNode.

Some XElement methods can be used from XAML. For more information, see LINQ to XML Dynamic Properties.

Constructors

X​Element(​XElement)

Initializes a new instance of the XElement class from another XElement object.

X​Element(​XName)

Initializes a new instance of the XElement class with the specified name.

X​Element(​XStreaming​Element)

Initializes a new instance of the XElement class from an XStreamingElement object.

X​Element(​XName, ​Object)

Initializes a new instance of the XElement class with the specified name and content.

X​Element(​XName, ​Object[])

Initializes a new instance of the XElement class with the specified name and content.

Properties

Empty​Sequence

Gets an empty collection of elements.

First​Attribute

Gets the first attribute of this element.

Has​Attributes

Gets a value indicating whether this element has at least one attribute.

Has​Elements

Gets a value indicating whether this element has at least one child element.

Is​Empty

Gets a value indicating whether this element contains no content.

Last​Attribute

Gets the last attribute of this element.

Name

Gets or sets the name of this element.

Node​Type

Gets the node type for this node.

Value

Gets or sets the concatenated text contents of this element.

Methods

Ancestors​And​Self()

Returns a collection of elements that contain this element, and the ancestors of this element.

Ancestors​And​Self(​XName)

Returns a filtered collection of elements that contain this element, and the ancestors of this element. Only elements that have a matching XName are included in the collection.

Attribute(​XName)

Returns the XAttribute of this XElement that has the specified XName.

Attributes()

Returns a collection of attributes of this element.

Attributes(​XName)

Returns a filtered collection of attributes of this element. Only elements that have a matching XName are included in the collection.

Descendant​Nodes​And​Self()

Returns a collection of nodes that contain this element, and all descendant nodes of this element, in document order.

Descendants​And​Self(​XName)

Returns a filtered collection of elements that contain this element, and all descendant elements of this element, in document order. Only elements that have a matching XName are included in the collection.

Descendants​And​Self()

Returns a collection of elements that contain this element, and all descendant elements of this element, in document order.

Get​Default​Namespace()

Gets the default XNamespace of this XElement.

Get​Namespace​Of​Prefix(​String)

Gets the namespace associated with a particular prefix for this XElement.

Get​Prefix​Of​Namespace(​XNamespace)

Gets the prefix associated with a namespace for this XElement.

Load(​Text​Reader, ​Load​Options)

Loads an XElement from a TextReader, optionally preserving white space and retaining line information.

Load(​Xml​Reader, ​Load​Options)

Loads an XElement from an XmlReader, optionally preserving white space, setting the base URI, and retaining line information.

Load(​String, ​Load​Options)

Loads an XElement from a file, optionally preserving white space, setting the base URI, and retaining line information.

Load(​Stream, ​Load​Options)

Creates a new XElement instance by using the specified stream, optionally preserving white space, setting the base URI, and retaining line information.

Load(​Text​Reader)

Loads an XElement from a TextReader.

Load(​String)

Loads an XElement from a file.

Load(​Stream)

Creates a new XElement instance by using the specified stream.

Load(​Xml​Reader)

Loads an XElement from an XmlReader.

Load​Async(​Stream, ​Load​Options, ​Cancellation​Token)
Load​Async(​Text​Reader, ​Load​Options, ​Cancellation​Token)
Load​Async(​Xml​Reader, ​Load​Options, ​Cancellation​Token)
Parse(​String)

Load an XElement from a string that contains XML.

Parse(​String, ​Load​Options)

Load an XElement from a string that contains XML, optionally preserving white space and retaining line information.

Remove​All()

Removes nodes and attributes from this XElement.

Remove​Attributes()

Removes the attributes of this XElement.

Replace​All(​Object)

Replaces the child nodes and the attributes of this element with the specified content.

Replace​All(​Object[])

Replaces the child nodes and the attributes of this element with the specified content.

Replace​Attributes(​Object[])

Replaces the attributes of this element with the specified content.

Replace​Attributes(​Object)

Replaces the attributes of this element with the specified content.

Save(​Stream)

Outputs this XElement to the specified Stream.

Save(​Text​Writer)

Serialize this element to a TextWriter.

Save(​String)

Serialize this element to a file.

Save(​Xml​Writer)

Serialize this element to an XmlWriter.

Save(​Stream, ​Save​Options)

Outputs this XElement to the specified Stream, optionally specifying formatting behavior.

Save(​Text​Writer, ​Save​Options)

Serialize this element to a TextWriter, optionally disabling formatting.

Save(​String, ​Save​Options)

Serialize this element to a file, optionally disabling formatting.

Save​Async(​Stream, ​Save​Options, ​Cancellation​Token)
Save​Async(​Xml​Writer, ​Cancellation​Token)
Save​Async(​Text​Writer, ​Save​Options, ​Cancellation​Token)
Set​Attribute​Value(​XName, ​Object)

Sets the value of an attribute, adds an attribute, or removes an attribute.

Set​Element​Value(​XName, ​Object)

Sets the value of a child element, adds a child element, or removes a child element.

Set​Value(​Object)

Sets the value of this element.

Write​To(​Xml​Writer)

Write this element to an XmlWriter.

Write​To​Async(​Xml​Writer, ​Cancellation​Token)
I​Xml​Serializable.​Get​Schema()

Gets an XML schema definition that describes the XML representation of this object.

I​Xml​Serializable.​Read​Xml(​Xml​Reader)

Generates an object from its XML representation.

I​Xml​Serializable.​Write​Xml(​Xml​Writer)

Converts an object into its XML representation.

Operators

op_​Explicit(​XElement to ​Nullable<​Int64>)
op_​Explicit(​XElement to ​Nullable<​Single>)
op_​Explicit(​XElement to ​Nullable<​Time​Span>)
op_​Explicit(​XElement to ​Nullable<​UInt32>)
op_​Explicit(​XElement to ​Nullable<​UInt64>)
op_​Explicit(​XElement to ​UInt32)
op_​Explicit(​XElement to ​String)
op_​Explicit(​XElement to ​Time​Span)
op_​Explicit(​XElement to ​UInt64)
op_​Explicit(​XElement to ​Nullable<​Int32>)
op_​Explicit(​XElement to ​Single)
op_​Explicit(​XElement to ​Nullable<​Guid>)
op_​Explicit(​XElement to ​Nullable<​Boolean>)
op_​Explicit(​XElement to ​Nullable<​Decimal>)
op_​Explicit(​XElement to ​Boolean)
op_​Explicit(​XElement to ​Date​Time)
op_​Explicit(​XElement to ​Date​Time​Offset)
op_​Explicit(​XElement to ​Double)
op_​Explicit(​XElement to ​Guid)
op_​Explicit(​XElement to ​Decimal)
op_​Explicit(​XElement to ​Int64)
op_​Explicit(​XElement to ​Nullable<​Double>)
op_​Explicit(​XElement to ​Nullable<​Date​Time>)
op_​Explicit(​XElement to ​Nullable<​Date​Time​Offset>)
op_​Explicit(​XElement to ​Int32)