Extensions.Attributes 方法

定義

傳回來源集合中每個項目的屬性集合。

多載

Attributes(IEnumerable<XElement>)

傳回來源集合中每個項目的屬性集合。

Attributes(IEnumerable<XElement>, XName)

傳回來源集合中每個項目之屬性的已篩選集合。 集合中只會包含具有相符之 XName 的項目。

備註

Visual Basic使用者可以使用整合式屬性座標軸,從專案集合中擷取具有特定名稱的屬性。

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

Attributes(IEnumerable<XElement>)

傳回來源集合中每個項目的屬性集合。

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source);
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement?> source);

參數

source
IEnumerable<XElement>

IEnumerable<T>XElement,其中包含來源集合。

傳回

IEnumerable<XAttribute>

IEnumerable<T>XAttribute,其中包含來源集合中每個項目的屬性。

範例

下列範例會擷取專案集合,然後擷取集合中所有專案之所有屬性的集合。 請注意,產生的集合只包含 和 元素的屬性 Child1 ,而不包含 元素的屬性 RootChild2

請注意,這個方法會傳回命名空間屬性。

C#
XElement xmlTree = new XElement("Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XElement("Child1",  
        new XAttribute("Att1", "content3"),  
        new XAttribute("Att2", "content4")  
    ),  
    new XElement("Child2",  
        new XAttribute("Att1", "content5"),  
        new XAttribute("Att2", "content6")  
    )  
);  
Console.WriteLine(xmlTree);  
Console.WriteLine("-----");  

IEnumerable<XAttribute> attList =  
    from att in xmlTree.DescendantsAndSelf().Attributes()  
    select att;  

foreach (XAttribute att in attList)  
    Console.WriteLine(att);  

這個範例會產生下列輸出:

<Root xmlns:aw="http://www.adventure-works.com" Att1="content1" Att2="content2">  
  <Child1 Att1="content3" Att2="content4" />  
  <Child2 Att1="content5" Att2="content6" />  
</Root>  
-----  
xmlns:aw="http://www.adventure-works.com"  
Att1="content1"  
Att2="content2"  
Att1="content3"  
Att2="content4"  
Att1="content5"  
Att2="content6"  

以下是相同的範例,但在此情況下,XML 位於命名空間中。 如需詳細資訊,請參閱 使用 XML 命名空間。 請注意,命名空間屬性包含在傳回的集合中。

C#
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"),  
    new XElement(aw + "Child1",  
        new XAttribute(aw + "Att1", "content3"),  
        new XAttribute(aw + "Att2", "content4")  
    ),  
    new XElement(aw + "Child2",  
        new XAttribute(aw + "Att1", "content5"),  
        new XAttribute(aw + "Att2", "content6")  
    )  
);  
Console.WriteLine(xmlTree);  
Console.WriteLine("-----");  

IEnumerable<XAttribute> attList =  
    from att in xmlTree.DescendantsAndSelf().Attributes()  
    select att;  

foreach (XAttribute att in attList)  
    Console.WriteLine(att);  

這個範例會產生下列輸出:

<aw:Root xmlns:aw="http://www.adventure-works.com" aw:Att1="content1" aw:Att2="content2">  
  <aw:Child1 aw:Att1="content3" aw:Att2="content4" />  
  <aw:Child2 aw:Att1="content5" aw:Att2="content6" />  
</aw:Root>  
-----  
xmlns:aw="http://www.adventure-works.com"  
aw:Att1="content1"  
aw:Att2="content2"  
aw:Att1="content3"  
aw:Att2="content4"  
aw:Att1="content5"  
aw:Att2="content6"  

備註

請注意,不同于某些其他 XML 程式設計介面,在 LINQ to XML 中,命名空間會呈現為屬性。

雖然Visual Basic使用者可以使用整合式屬性座標軸,從專案集合中擷取具有指定名稱的屬性,但沒有整合式Visual Basic軸可擷取集合中所有元素的所有屬性。

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

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Attributes(IEnumerable<XElement>, XName)

傳回來源集合中每個項目之屬性的已篩選集合。 集合中只會包含具有相符之 XName 的項目。

C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source, System.Xml.Linq.XName name);
C#
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement?> source, System.Xml.Linq.XName? name);

參數

source
IEnumerable<XElement>

IEnumerable<T>XElement,其中包含來源集合。

name
XName

要比對的 XName

傳回

IEnumerable<XAttribute>

IEnumerable<T>XAttribute,其中包含來源集合中每個項目之屬性的已篩選集合。 集合中只會包含具有相符之 XName 的項目。

範例

下列範例會擷取 元素的集合,在此案例中會包含 Child1Child2 元素。 然後,它會擷取該子集合的所有屬性,其名稱為 Att1

C#
XElement xmlTree = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XElement("Child1",  
        new XAttribute("Att1", "content3"),  
        new XAttribute("Att2", "content4")  
    ),  
    new XElement("Child2",  
        new XAttribute("Att1", "content5"),  
        new XAttribute("Att2", "content6")  
    )  
);  

IEnumerable<XAttribute> attList = from att in xmlTree.Elements().Attributes("Att1")  
                                  select att;  

foreach (XAttribute att in attList)  
    Console.WriteLine(att);  

這個範例會產生下列輸出:

Att1="content3"  
Att1="content5"  

備註

請注意,不同于某些其他 XML 程式設計介面,在 LINQ to XML 中,命名空間會呈現為屬性。

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

另請參閱

適用於

.NET 7 和其他版本
產品 版本
.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
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0