XElement XElement XElement XElement Class
Definition
Represents an XML element. See XElement Class Overview and the Remarks section on this page for usage information and examples.
public ref class XElement : System::Xml::Linq::XContainer, System::Xml::Serialization::IXmlSerializable
public class XElement : System.Xml.Linq.XContainer, System.Xml.Serialization.IXmlSerializable
type XElement = class
inherit XContainer
interface IXmlSerializable
Public Class XElement
Inherits XContainer
Implements IXmlSerializable
- Inheritance
-
XElementXElementXElementXElement
- Implements
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
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
XElement(XElement) XElement(XElement) XElement(XElement) XElement(XElement) |
Initializes a new instance of the XElement class from another XElement object. |
XElement(XName) XElement(XName) XElement(XName) XElement(XName) |
Initializes a new instance of the XElement class with the specified name. |
XElement(XName, Object) XElement(XName, Object) XElement(XName, Object) XElement(XName, Object) |
Initializes a new instance of the XElement class with the specified name and content. |
XElement(XName, Object[]) XElement(XName, Object[]) XElement(XName, Object[]) XElement(XName, Object[]) |
Initializes a new instance of the XElement class with the specified name and content. |
XElement(XStreamingElement) XElement(XStreamingElement) XElement(XStreamingElement) XElement(XStreamingElement) |
Initializes a new instance of the XElement class from an XStreamingElement object. |
Properties
BaseUri BaseUri BaseUri BaseUri |
Gets the base URI for this XObject. (Inherited from XObject) |
Document Document Document Document |
Gets the XDocument for this XObject. (Inherited from XObject) |
EmptySequence EmptySequence EmptySequence EmptySequence |
Gets an empty collection of elements. |
FirstAttribute FirstAttribute FirstAttribute FirstAttribute |
Gets the first attribute of this element. |
FirstNode FirstNode FirstNode FirstNode |
Gets the first child node of this node. (Inherited from XContainer) |
HasAttributes HasAttributes HasAttributes HasAttributes |
Gets a value indicating whether this element has at least one attribute. |
HasElements HasElements HasElements HasElements |
Gets a value indicating whether this element has at least one child element. |
IsEmpty IsEmpty IsEmpty IsEmpty |
Gets a value indicating whether this element contains no content. |
IXmlLineInfo.LineNumber IXmlLineInfo.LineNumber IXmlLineInfo.LineNumber IXmlLineInfo.LineNumber |
Gets the line number that the underlying XmlReader reported for this XObject. (Inherited from XObject) |
IXmlLineInfo.LinePosition IXmlLineInfo.LinePosition IXmlLineInfo.LinePosition IXmlLineInfo.LinePosition |
Gets the line position that the underlying XmlReader reported for this XObject. (Inherited from XObject) |
LastAttribute LastAttribute LastAttribute LastAttribute |
Gets the last attribute of this element. |
LastNode LastNode LastNode LastNode |
Gets the last child node of this node. (Inherited from XContainer) |
Name Name Name Name |
Gets or sets the name of this element. |
NextNode NextNode NextNode NextNode |
Gets the next sibling node of this node. (Inherited from XNode) |
NodeType NodeType NodeType NodeType |
Gets the node type for this node. |
Parent Parent Parent Parent |
Gets the parent XElement of this XObject. (Inherited from XObject) |
PreviousNode PreviousNode PreviousNode PreviousNode |
Gets the previous sibling node of this node. (Inherited from XNode) |
Value Value Value Value |
Gets or sets the concatenated text contents of this element. |
Methods
Operators
Explicit Interface Implementations
IXmlLineInfo.HasLineInfo() IXmlLineInfo.HasLineInfo() IXmlLineInfo.HasLineInfo() IXmlLineInfo.HasLineInfo() |
Gets a value indicating whether or not this XObject has line information. (Inherited from XObject) |
IXmlSerializable.GetSchema() IXmlSerializable.GetSchema() IXmlSerializable.GetSchema() IXmlSerializable.GetSchema() |
Gets an XML schema definition that describes the XML representation of this object. |
IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) |
Generates an object from its XML representation. |
IXmlSerializable.WriteXml(XmlWriter) IXmlSerializable.WriteXml(XmlWriter) IXmlSerializable.WriteXml(XmlWriter) IXmlSerializable.WriteXml(XmlWriter) |
Converts an object into its XML representation. |
Events
Changed Changed Changed Changed |
Raised when this XObject or any of its descendants have changed. (Inherited from XObject) |
Changing Changing Changing Changing |
Raised when this XObject or any of its descendants are about to change. (Inherited from XObject) |
Extension Methods
Applies to
See also
Feedback
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.
Loading feedback...