Extensions.Elements 方法

定义

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

重载

Elements<T>(IEnumerable<T>)

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

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

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

注解

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

此方法使用延迟执行。

Elements<T>(IEnumerable<T>)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

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

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;

类型参数

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);  

该示例产生下面的输出:

<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);  

该示例产生下面的输出:

<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 的所有子元素,但没有集成元素轴可用于检索源集合中每个元素的每个子元素的集合。

此方法使用延迟执行。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

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

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;

类型参数

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);  

该示例产生下面的输出:

<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);  

该示例产生下面的输出:

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

注解

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

此方法使用延迟执行。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0