Extensions.Elements 方法

定义

返回源集合中每个元素和文档的子元素的集合。

重载

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。

Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。 集合中仅包括具有匹配 XName 的元素。

注解

Visual Basic 包含一个集成元素轴,使你能够查找具有为源集合中的每个元素指定的 XName 的所有子元素。

此方法使用延迟执行。

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。

public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements(System::Collections::Generic::IEnumerable<T> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T?> source) where T : System.Xml.Linq.XContainer;
static member Elements : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Elements(Of T As XContainer) (source As IEnumerable(Of T)) As IEnumerable(Of XElement)

类型参数

T

source 中对象的类型,被约束为 XContainer

参数

source
IEnumerable<T>

一个包含源集合的 IEnumerable<T>XElement

返回

源集合中每个元素或文档的子元素的 IEnumerable<T>XElement

示例

以下示例检索元素名称为 的元素集合 Child。 然后,它使用此轴方法检索集合的所有子元素。

XElement xmlTree = new XElement("Root",  
    new XElement("Child",  
        new XElement("GrandChild1", 1),  
        new XElement("GrandChild2", 2)  
    ),  
    new XElement("Child",  
        new XElement("GrandChild3", 3),  
        new XElement("GrandChild4", 4)  
    ),  
    new XElement("Child",  
        new XElement("GrandChild5", 5),  
        new XElement("GrandChild6", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements("Child").Elements()  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
     <Root>  
          <Child>  
              <GrandChild1>1</GrandChild1>  
              <GrandChild2>2</GrandChild2>  
          </Child>  

          <Child>  
              <GrandChild3>3</GrandChild3>  
              <GrandChild4>4</GrandChild4>  
          </Child>  

          <Child>  
              <GrandChild5>5</GrandChild5>  
              <GrandChild6>6</GrandChild6>  
          </Child>  
      </Root>  

Dim allGrandChildren = From el In xmlTree.<Child>.Elements _  
                       Select el  

For Each el As XElement In allGrandChildren  
    Console.WriteLine(el)  
Next  

该示例产生下面的输出:

<GrandChild1>1</GrandChild1>  
<GrandChild2>2</GrandChild2>  
<GrandChild3>3</GrandChild3>  
<GrandChild4>4</GrandChild4>  
<GrandChild5>5</GrandChild5>  
<GrandChild6>6</GrandChild6>  

下面是相同的示例,但在本例中,XML 位于命名空间中。 有关详细信息,请参阅 使用 XML 命名空间

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild1", 1),  
        new XElement(aw + "GrandChild2", 2)  
    ),  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild3", 3),  
        new XElement(aw + "GrandChild4", 4)  
    ),  
    new XElement(aw + "Child",  
        new XElement(aw + "GrandChild5", 5),  
        new XElement(aw + "GrandChild6", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements(aw + "Child").Elements()  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
             <Root>  
                 <Child>  
                     <GrandChild1>1</GrandChild1>  
                     <GrandChild2>2</GrandChild2>  
                 </Child>  

                 <Child>  
                     <GrandChild3>3</GrandChild3>  
                     <GrandChild4>4</GrandChild4>  
                 </Child>  

                 <Child>  
                     <GrandChild5>5</GrandChild5>  
                     <GrandChild6>6</GrandChild6>  
                 </Child>  
             </Root>  

        Dim allGrandChildren = From el In xmlTree.<Child>.Elements _  
                               Select el  

        For Each el As XElement In allGrandChildren  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

该示例产生下面的输出:

<GrandChild1 xmlns="http://www.adventure-works.com">1</GrandChild1>  
<GrandChild2 xmlns="http://www.adventure-works.com">2</GrandChild2>  
<GrandChild3 xmlns="http://www.adventure-works.com">3</GrandChild3>  
<GrandChild4 xmlns="http://www.adventure-works.com">4</GrandChild4>  
<GrandChild5 xmlns="http://www.adventure-works.com">5</GrandChild5>  
<GrandChild6 xmlns="http://www.adventure-works.com">6</GrandChild6>  

注解

尽管 Visual Basic 包含一个集成元素轴,用于查找源集合中每个元素的指定 XName 的所有子元素,但没有集成元素轴可用于检索源集合中每个元素的每个子元素的集合。

此方法使用延迟执行。

另请参阅

适用于

Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。 集合中仅包括具有匹配 XName 的元素。

public:
generic <typename T>
 where T : System::Xml::Linq::XContainer[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ Elements(System::Collections::Generic::IEnumerable<T> ^ source, System::Xml::Linq::XName ^ name);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T> source, System.Xml.Linq.XName name) where T : System.Xml.Linq.XContainer;
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> Elements<T> (this System.Collections.Generic.IEnumerable<T?> source, System.Xml.Linq.XName? name) where T : System.Xml.Linq.XContainer;
static member Elements : seq<'T (requires 'T :> System.Xml.Linq.XContainer)> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XElement> (requires 'T :> System.Xml.Linq.XContainer)
<Extension()>
Public Function Elements(Of T As XContainer) (source As IEnumerable(Of T), name As XName) As IEnumerable(Of XElement)

类型参数

T

source 中对象的类型,被约束为 XContainer

参数

source
IEnumerable<T>

一个包含源集合的 IEnumerable<T>XElement

name
XName

要匹配的 XName

返回

源集合中每个元素和文档的子元素的 IEnumerable<T>XElement。 集合中仅包括具有匹配 XName 的元素。

示例

如果要检索特定深度处具有指定名称的所有元素,此扩展方法非常有用。 如果文档非常常规,这很容易,但如果文档不规则,则可能会更困难一些。 在以下示例中,我们希望检索属于元素子级Item的所有aaa元素。 给定 Item 元素可能包含元素,也可能不包含 aaa 元素。 使用此扩展方法可以轻松完成此操作,如下所示:

XElement xmlTree = new XElement("Root",  
    new XElement("Item",  
        new XElement("aaa", 1),  
        new XElement("bbb", 2)  
    ),  
    new XElement("Item",  
        new XElement("ccc", 3),  
        new XElement("aaa", 4)  
    ),  
    new XElement("Item",  
        new XElement("ddd", 5),  
        new XElement("eee", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements("Item").Elements("aaa")  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Dim xmlTree As XElement = _  
    <Root>  
        <Item>  
            <aaa>1</aaa>  
            <bbb>2</bbb>  
        </Item>  

        <Item>  
            <ccc>3</ccc>  
            <aaa>4</aaa>  
        </Item>  

        <Item>  
            <ddd>5</ddd>  
            <eee>6</eee>  
        </Item>  
    </Root>  

Dim allGrandChildren = From el In xmlTree.<Item>.<aaa> _  
                       Select el  

For Each el As XElement In allGrandChildren  
    Console.WriteLine(el)  
Next  

该示例产生下面的输出:

<aaa>1</aaa>  
<aaa>4</aaa>  

下面是相同的示例,但在本例中,XML 位于命名空间中。 有关详细信息,请参阅 使用 XML 命名空间

XNamespace aw = "http://www.adventure-works.com";  
XElement xmlTree = new XElement(aw + "Root",  
    new XElement(aw + "Item",  
        new XElement(aw + "aaa", 1),  
        new XElement(aw + "bbb", 2)  
    ),  
    new XElement(aw + "Item",  
        new XElement(aw + "ccc", 3),  
        new XElement(aw + "aaa", 4)  
    ),  
    new XElement(aw + "Item",  
        new XElement(aw + "ddd", 5),  
        new XElement(aw + "eee", 6)  
    )  
);  

IEnumerable<XElement> allGrandChildren =  
    from el in xmlTree.Elements(aw + "Item").Elements(aw + "aaa")  
    select el;  

foreach (XElement el in allGrandChildren)  
    Console.WriteLine(el);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <Root>  
                <Item>  
                    <aaa>1</aaa>  
                    <bbb>2</bbb>  
                </Item>  

                <Item>  
                    <ccc>3</ccc>  
                    <aaa>4</aaa>  
                </Item>  

                <Item>  
                    <ddd>5</ddd>  
                    <eee>6</eee>  
                </Item>  
            </Root>  

        Dim allGrandChildren = From el In xmlTree.<Item>.<aaa> _  
                               Select el  

        For Each el As XElement In allGrandChildren  
            Console.WriteLine(el)  
        Next  
    End Sub  
End Module  

该示例产生下面的输出:

<aaa xmlns="http://www.adventure-works.com">1</aaa>  
<aaa xmlns="http://www.adventure-works.com">4</aaa>  

注解

Visual Basic 用户可以使用集成元素轴检索集合中每个元素的子元素。

此方法使用延迟执行。

另请参阅

适用于