XElement.Attributes 方法

定義

回傳該元素的一組屬性。

多載

名稱 Description
Attributes()

回傳該元素的一組屬性。

Attributes(XName)

回傳該元素的篩選屬性集合。 只有與 Match 相符 XName 的屬性才會被納入集合中。

備註

此方法使用延遲執行。

Attributes()

來源:
XElement.cs
來源:
XElement.cs
來源:
XElement.cs
來源:
XElement.cs
來源:
XElement.cs

回傳該元素的一組屬性。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes();
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes();
member this.Attributes : unit -> seq<System.Xml.Linq.XAttribute>
Public Function Attributes () As IEnumerable(Of XAttribute)

傳回

IEnumerable<T>XAttribute元素的屬性之一。

範例

以下範例建立一個具有兩個屬性的元素。 接著它會用這個方法取得該元素的所有屬性。

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "content1"),
    new XAttribute("Att2", "content2")
);
IEnumerable<XAttribute> attList =
    from at in xmlTree.Attributes()
    select at;
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>

Dim attList As IEnumerable(Of XAttribute) = _
From at In xmlTree.Attributes() _
Select at

For Each att In attList
    Console.WriteLine(att)
Next

此範例會產生下列輸出:

Att1="content1"
Att2="content2"

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

XNamespace aw = "http://www.adventure-works.com";
XElement xmlTree = new XElement(aw + "Root",
    new XAttribute(aw + "Att1", "content1"),
    new XAttribute(aw + "Att2", "content2"),
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com")
);
IEnumerable<XAttribute> attList =
    from at in xmlTree.Attributes()
    select at;
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = <aw:Root aw:Att1="content1" aw:Att2="content2"/>

        Dim attList As IEnumerable(Of XAttribute) = _
            From at In xmlTree.Attributes() _
            Select at

        For Each att In attList
            Console.WriteLine(att)
        Next
    End Sub
End Module

此範例會產生下列輸出:

aw:Att1="content1"
aw:Att2="content2"
xmlns:aw="http://www.adventure-works.com"

備註

回傳集合中的屬性依加入元素的順序排列。 若 XML 樹是從 XML 解析而來,屬性會依文件順序回傳。

此方法使用延遲執行。

另請參閱

適用於

Attributes(XName)

來源:
XElement.cs
來源:
XElement.cs
來源:
XElement.cs
來源:
XElement.cs
來源:
XElement.cs

回傳該元素的篩選屬性集合。 只有與 Match 相符 XName 的屬性才會被納入集合中。

public:
 System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Xml::Linq::XName ^ name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes(System.Xml.Linq.XName name);
public System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes(System.Xml.Linq.XName? name);
member this.Attributes : System.Xml.Linq.XName -> seq<System.Xml.Linq.XAttribute>
Public Function Attributes (name As XName) As IEnumerable(Of XAttribute)

參數

name
XName

那就是 XName 匹配的。

傳回

其中的 IEnumerable<T>XAttribute 包含該元素的屬性。 只有與 Match 相符 XName 的屬性才會被納入集合中。

範例

以下範例使用了此方法。

XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "content1"),
    new XAttribute("Att2", "content2")
);
IEnumerable<XAttribute> attList = xmlTree.Attributes("Att1");
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>

Dim attList As IEnumerable(Of XAttribute) = xmlTree.Attributes("Att1")

For Each att In attList
    Console.WriteLine(att)
Next

此範例會產生下列輸出:

Att1="content1"

以下是相同的範例,但此例中 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 XAttribute(aw + "Att1", "content1"),
    new XAttribute(aw + "Att2", "content2")
);
IEnumerable<XAttribute> attList = xmlTree.Attributes(aw + "Att1");
foreach (XAttribute att in attList)
    Console.WriteLine(att);
Imports <xmlns:aw="http://www.adventure-works.com">

Module Module1
    Sub Main()
        Dim xmlTree As XElement = <aw:Root aw:Att1="content1" aw:Att2="content2"/>

        Dim attList As IEnumerable(Of XAttribute) = xmlTree.Attributes(GetXmlNamespace(aw) + "Att1")

        For Each att In attList
            Console.WriteLine(att)
        Next
    End Sub
End Module

此範例會產生下列輸出:

aw:Att1="content1"

備註

屬性名稱必須在元素中唯一。 因此,它可以回傳只包含一個屬性的集合,或是回傳空集合。

此方法使用延遲執行。

另請參閱

適用於