.NET Framework Class Library
XElement Class

Represents an XML element.

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)
Syntax

Visual Basic (Declaration)
Public Class XElement _
    Inherits XContainer _
    Implements IXmlSerializable
Visual Basic (Usage)
Dim instance As XElement
C#
public class XElement : XContainer, IXmlSerializable
Visual C++
public ref class XElement : public XContainer, 
    IXmlSerializable
JScript
public class XElement extends XContainer implements IXmlSerializable
Remarks

This class represents an XML element, the fundamental XML construct.

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.

Examples

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

C#
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);
Visual Basic
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:

xmlLang
<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.

C#
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);
Visual Basic
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:

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

System..::.Object
  System.Xml.Linq..::.XObject
    System.Xml.Linq..::.XNode
      System.Xml.Linq..::.XContainer
        System.Xml.Linq..::.XElement
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0
See Also

Reference

Other Resources

Tags :


Page view tracker