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<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<XElement>

來源集合中每個項目和文件之 IEnumerable<T>XElement。 集合中只會包含具有相符之 XName 的項目。

範例

當您想要在特定深度擷取具有指定名稱的所有專案時,這個擴充方法很有用。 如果檔非常一般,但這很容易,但如果檔不規則,則可能會比較困難。 在下列範例中,我們想要擷取 aaa 所有屬於元素子系的專案 Item 。 指定的 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使用者可以使用整合專案座標軸來擷取集合中每個元素的子項目。

這個方法會使用延後的執行。

另請參閱

適用於