XContainer.Elements 方法

定義

回傳該元素或文件的子元素集合,依文件順序排列。

多載

名稱 Description
Elements()

回傳該元素或文件的子元素集合,依文件順序排列。

Elements(XName)

回傳該元素或文件的子元素篩選後的集合,依文件順序排列。 集合中只會包含具有相符 XName 的專案。

備註

此方法使用延遲執行。

Elements()

來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs

回傳該元素或文件的子元素集合,依文件順序排列。

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子元素的 ,依文件順序排列。

範例

以下範例建立 XML 樹,並使用此軸方法選擇部分元素。

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

此範例會產生下列輸出:

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

以下是相同的範例,但此例中 XML 位於命名空間中。 欲了解更多資訊,請參閱 XML 命名空間工作

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

此範例會產生下列輸出:

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

備註

此方法使用延遲執行。

另請參閱

適用於

Elements(XName)

來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs
來源:
XContainer.cs

回傳該元素或文件的子元素篩選後的集合,依文件順序排列。 集合中只會包含具有相符 XName 的專案。

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

那就是 XName 匹配的。

傳回

一個IEnumerable<T>XElement包含具有匹配 XContainer、且按文件順序排列的子節點XName的 。

範例

以下範例建立 XML 樹,並使用此軸方法選擇多個子元素。

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

此範例會產生下列輸出:

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

以下是相同的範例,但此例中 XML 位於命名空間中。 欲了解更多資訊,請參閱 XML 命名空間工作

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

此範例會產生下列輸出:

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

備註

此方法使用延遲執行。

另請參閱

適用於