Extensions.Elements 方法

定义

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

重载

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

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

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

注解

Visual Basic 包含一个集成元素轴,可用于查找所有子元素,每个元素XName都具有为源集合中的每个元素指定的。Visual Basic contains an integrated elements axis that allows you to find all child elements with a specified XName for every element in the source collection.

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

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

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;
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 中对象的类型,被约束为 XContainerThe type of the objects in source, constrained to XContainer.

参数

source
IEnumerable<T>

一个包含源集合的 IEnumerable<T>XElementAn IEnumerable<T> of XElement that contains the source collection.

返回

源集合中每个元素或文档的子元素的 IEnumerable<T>XElementAn IEnumerable<T> of XElement of the child elements of every element or document in the source collection.

示例

下面的示例检索元素名称为的Child元素的集合。The following example retrieves a collection of elements with the element name of Child. 然后,它使用此轴方法检索集合中的所有子元素。It then uses this axis method to retrieve all child elements of the collection.

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  
  

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

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

下面是同一个示例,但在这种情况下,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 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  
  

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

<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个元素指定的指定的所有子元素,但没有集成的元素轴可用于检索每个源集合中的每个元素的子元素。Although Visual Basic contains an integrated elements axis that allows you to find all child elements with a specified XName for every element in the source collection, there is no integrated elements axis that allows you to retrieve a collection of every child element for every element in the source collection.

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

另请参阅

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

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

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;
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 中对象的类型,被约束为 XContainerThe type of the objects in source, constrained to XContainer.

参数

source
IEnumerable<T>

一个包含源集合的 IEnumerable<T>XElementAn IEnumerable<T> of XElement that contains the source collection.

name
XName

要匹配的 XNameThe XName to match.

返回

源集合中每个元素和文档的子元素的 IEnumerable<T>XElementAn IEnumerable<T> of XElement of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

示例

如果要在特定深度检索具有指定名称的所有元素,则此扩展方法非常有用。This extension method is useful when you want to retrieve all elements with a specified name at a particular depth. 如果文档非常普通,则这种方法很简单,但如果文档不稳定,则可能会比较困难。This is easy if the document is very regular, but if the document is irregular, it can be a bit more difficult. 在下面的示例中,我们要检索作为aaa Item元素的子元素的所有元素。In the following example, we want to retrieve all aaa elements that are children of Item elements. 给定Item元素可以aaa包含也可以不包含元素。A given Item element may or may not contain an aaa element. 使用此扩展方法可以轻松完成此操作,如下所示:This is easily accomplished using this extension method, as follows:

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  
  

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

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

下面是同一个示例,但在这种情况下,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 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  
  

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

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

注解

Visual Basic 用户可以使用 "集成元素" 轴检索集合中每个元素的子元素。Visual Basic users can use the integrated elements axis to retrieve the child elements of every element in a collection.

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

另请参阅

适用于