XElement.Attributes 方法

定義

傳回這個元素的屬性集合。

多載

Attributes()

傳回這個元素的屬性集合。

Attributes(XName)

傳回這個元素的已篩選屬性集合。 集合中只會包含具有相符之 XName 的屬性。

備註

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

Attributes()

傳回這個元素的屬性集合。

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)

傳回這個元素的已篩選屬性集合。 集合中只會包含具有相符之 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,包含這個項目的屬性。 集合中只會包含具有相符之 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"

備註

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

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

另請參閱

適用於