XContainer.Elements 方法

定义

按文档顺序返回此元素或文档的子元素集合。Returns a collection of the child elements of this element or document, in document order.

重载

Elements()

按文档顺序返回此元素或文档的子元素集合。Returns a collection of the child elements of this element or document, in document order.

Elements(XName)

按文档顺序返回此元素或文档的已筛选的子元素集合。Returns a filtered collection of the child elements of this element or document, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

注解

此方法使用延迟执行。This method uses deferred execution.

Elements()

按文档顺序返回此元素或文档的子元素集合。Returns a collection of the child elements of this element or document, in document order.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements ();
member this.Elements : unit -> seq<System.Xml.Linq.XElement>
Public Function Elements () As IEnumerable(Of XElement)

返回

IEnumerable<T>XElement,其中按文档顺序包含此 XContainer 的子元素。An IEnumerable<T> of XElement containing the child elements of this XContainer, in document order.

示例

下面的示例创建一个 XML 树,然后使用此轴方法选择一些元素。The following example creates an XML tree, and then selects some elements using this axis method.

XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XElement("Child5", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements()  
    where (int)el <= 3  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
        <Root>  
            <Child1>1</Child1>  
            <Child2>2</Child2>  
            <Child3>3</Child3>  
            <Child4>4</Child4>  
            <Child5>5</Child5>  
        </Root>  
  
Dim elements = From el In xmlTree.Elements _  
               Where el.Value <= 3 _  
               Select el  
  
For Each el In elements  
    Console.WriteLine(el)  
Next  

该示例产生下面的输出:This example produces the following output:

<Child1>1</Child1>  
<Child2>2</Child2>  
<Child3>3</Child3>  

下面是同一个示例,但在这种情况下,XML 位于命名空间中。The following is the same example, but in this case the XML is in a namespace. 有关详细信息,请参阅使用XML 命名空间For more information, see Working with XML Namespaces.

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    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)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements()  
    where (int)el <= 3  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Imports <xmlns:aw="http://www.adventure-works.com">  
  
Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <aw:Root>  
                <aw:Child1>1</aw:Child1>  
                <aw:Child2>2</aw:Child2>  
                <aw:Child3>3</aw:Child3>  
                <aw:Child4>4</aw:Child4>  
                <aw:Child5>5</aw:Child5>  
            </aw:Root>  
  
        Dim elements = From el In xmlTree.Elements _  
                       Where el.Value <= 3 _  
                       Select el  
  
        For Each el In elements  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

该示例产生下面的输出:This example produces the following output:

<aw:Child1 xmlns:aw="http://www.adventure-works.com">1</aw:Child1>  
<aw:Child2 xmlns:aw="http://www.adventure-works.com">2</aw:Child2>  
<aw:Child3 xmlns:aw="http://www.adventure-works.com">3</aw:Child3>  

注解

此方法使用延迟执行。This method uses deferred execution.

另请参阅

Elements(XName)

按文档顺序返回此元素或文档的已筛选的子元素集合。Returns a filtered collection of the child elements of this element or document, in document order. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements (System.Xml.Linq.XName name);
member this.Elements : System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement>
Public Function Elements (name As XName) As IEnumerable(Of XElement)

参数

name
XName

要匹配的 XNameThe XName to match.

返回

IEnumerable<T>XElement,其中按文档顺序包含具有匹配 XContainerXName 的子级。An IEnumerable<T> of XElement containing the children of the XContainer that have a matching XName, in document order.

示例

下面的示例创建一个 XML 树,然后使用此轴方法选择多个子元素。The following example creates an XML tree, and then selects several child elements using this axis method.

XElement xmlTree = new XElement("Root",  
    new XElement("Type1", 1),  
    new XElement("Type1", 2),  
    new XElement("Type2", 3),  
    new XElement("Type2", 4),  
    new XElement("Type2", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements("Type2")  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _   
        <Root>  
            <Type1>1</Type1>  
            <Type1>2</Type1>  
            <Type2>3</Type2>  
            <Type2>4</Type2>  
            <Type2>5</Type2>  
        </Root>  
  
Dim elements = From el In xmlTree.<Type2> _  
               Select el  
  
For Each el In elements  
    Console.WriteLine(el)  
Next  

该示例产生下面的输出:This example produces the following output:

<Type2>3</Type2>  
<Type2>4</Type2>  
<Type2>5</Type2>  

下面是同一个示例,但在这种情况下,XML 位于命名空间中。The following is the same example, but in this case the XML is in a namespace. 有关详细信息,请参阅使用XML 命名空间For more information, see Working with XML Namespaces.

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XElement(aw + "Type1", 1),  
    new XElement(aw + "Type1", 2),  
    new XElement(aw + "Type2", 3),  
    new XElement(aw + "Type2", 4),  
    new XElement(aw + "Type2", 5)  
);  
IEnumerable<XElement> elements =  
    from el in xmlTree.Elements(aw + "Type2")  
    select el;  
foreach (XElement el in elements)  
    Console.WriteLine(el);  
Imports <xmlns:aw="http://www.adventure-works.com">  
  
Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _   
            <aw:Root>  
                <aw:Type1>1</aw:Type1>  
                <aw:Type1>2</aw:Type1>  
                <aw:Type2>3</aw:Type2>  
                <aw:Type2>4</aw:Type2>  
                <aw:Type2>5</aw:Type2>  
            </aw:Root>  
  
        Dim elements = From el In xmlTree.<aw:Type2> _  
                       Select el  
  
        For Each el In elements  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

该示例产生下面的输出:This example produces the following output:

<aw:Type2 xmlns:aw="http://www.adventure-works.com">3</aw:Type2>  
<aw:Type2 xmlns:aw="http://www.adventure-works.com">4</aw:Type2>  
<aw:Type2 xmlns:aw="http://www.adventure-works.com">5</aw:Type2>  

注解

此方法使用延迟执行。This method uses deferred execution.

另请参阅

适用于