XElement (Clase) (System.Xml.Linq)

Cambiar vista:
Sin script
Biblioteca de clases de .NET Framework
XElement (Clase)

Actualización: noviembre 2007

Representa un elemento HTML.

Espacio de nombres:  System.Xml.Linq
Ensamblado:  System.Xml.Linq (en System.Xml.Linq.dll)
Sintaxis

Visual Basic (Declaración)
Public Class XElement _
	Inherits XContainer _
	Implements IXmlSerializable
Visual Basic (Uso)
Dim instance As XElement
C#
public class XElement : XContainer, IXmlSerializable
Visual C++
public ref class XElement : public XContainer, 
	IXmlSerializable
J#
public class XElement extends XContainer implements IXmlSerializable
JScript
public class XElement extends XContainer implements IXmlSerializable
Comentarios

Esta clase representa un elemento XML, la construcción XML fundamental.

Un elemento tiene un objeto XName, puede tener uno o varios atributos y, de manera opcional, puede incluir el contenido (para obtener más información, vea Nodes).

XElement puede incluir los siguientes tipos de contenido:

Para obtener información detallada sobre el contenido válido de XElement, vea Contenido válido de objetos XElement y XDocument.

XElement deriva de XContainer, que deriva de XNode.

Algunos métodos XElement se pueden usar desde XAML. Para obtener más información, vea Propiedades dinámicas de LINQ to XML.

Ejemplos

En el siguiente ejemplo se crea un árbol XML. El contenido del nuevo elemento proviene de una consulta LINQ.

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)

Este ejemplo produce el siguiente resultado:

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

A continuación se muestra el mismo ejemplo, pero en este caso el XML está en un espacio de nombres. Para obtener más información, vea Trabajar con espacios de nombres XML.

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

Este ejemplo produce el siguiente resultado:

xmlLang
<Root xmlns="http://www.adventure-works.com">
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>
Jerarquía de herencia

System.Object
  System.Xml.Linq.XObject
    System.Xml.Linq.XNode
      System.Xml.Linq.XContainer
        System.Xml.Linq.XElement
Seguridad para subprocesos

Todos los miembros static (Shared en Visual Basic) públicos de este tipo son seguros para la ejecución de subprocesos. No se garantiza que los miembros de instancias sean seguros para la ejecución de subprocesos.
Plataformas

Windows Vista, Windows XP SP2, Windows Server 2003, Windows CE, Windows Mobile para Smartphone, Windows Mobile para Pocket PC

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.
Información de versión

.NET Framework

Compatible con: 3.5

.NET Compact Framework

Compatible con: 3.5
Vea también

Referencia

Otros recursos