Extensions.Attributes Extensions.Attributes Extensions.Attributes Extensions.Attributes Method

定義

傳回來源集合中每個項目的屬性集合。Returns a collection of the attributes of every element in the source collection.

多載

Attributes(IEnumerable<XElement>) Attributes(IEnumerable<XElement>) Attributes(IEnumerable<XElement>) Attributes(IEnumerable<XElement>)

傳回來源集合中每個項目的屬性集合。Returns a collection of the attributes of every element in the source collection.

Attributes(IEnumerable<XElement>, XName) Attributes(IEnumerable<XElement>, XName) Attributes(IEnumerable<XElement>, XName) Attributes(IEnumerable<XElement>, XName)

傳回來源集合中每個項目之屬性的已篩選集合。Returns a filtered collection of the attributes of every element in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

備註

Visual Basic 使用者可以使用整合式的屬性軸,以擷取具有特定名稱的屬性集合中的項目。Visual Basic users can use the integrated attribute axis to retrieve attributes with a particular name from a collection of elements.

這個方法會使用延後的執行。This method uses deferred execution.

Attributes(IEnumerable<XElement>) Attributes(IEnumerable<XElement>) Attributes(IEnumerable<XElement>) Attributes(IEnumerable<XElement>)

傳回來源集合中每個項目的屬性集合。Returns a collection of the attributes of every element in the source collection.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ source);
public static System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute> Attributes (this System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement> source);
static member Attributes : seq<System.Xml.Linq.XElement> -> seq<System.Xml.Linq.XAttribute>
<Extension()>
Public Function Attributes (source As IEnumerable(Of XElement)) As IEnumerable(Of XAttribute)

參數

source
IEnumerable<XElement>

IEnumerable<T>XElement,其中包含來源集合。An IEnumerable<T> of XElement that contains the source collection.

傳回

IEnumerable<T>XAttribute,其中包含來源集合中每個項目的屬性。An IEnumerable<T> of XAttribute that contains the attributes of every element in the source collection.

範例

下列範例會擷取元素的集合,並接著會擷取所有屬性集合中的所有項目的集合。The following example retrieves a collection of elements, and then retrieves a collection of all attributes of all elements in the collection. 請注意,產生的集合包含的屬性Child1Child2項目和不屬性Root項目。Note that the resulting collection includes only the attributes of the Child1 and Child2 elements, and not the attributes of the Root element.

請注意,這個方法所傳回的命名空間屬性。Note that the namespace attribute is returned by this method.

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);  
Dim xmlTree As XElement = _  
    <Root xmlns:aw="http://www.adventure-works.com" Att1="content1" Att2="content2">  
        <Child1 Att1="content3" Att2="content4"/>  
        <Child2 Att1="content5" Att2="content6"/>  
    </Root>  

Dim attList = _  
    From att In xmlTree.DescendantsAndSelf.Attributes _  
    Select att  

Console.WriteLine(xmlTree)  
Console.WriteLine("-----")  

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

這個範例會產生下列輸出:This example produces the following output:

<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 位於命名空間。The following is the same example, but in this case the XML is in a namespace. 如需詳細資訊,請參閱 < 處理 XML 命名空間For more information, see Working with XML Namespaces. 請注意,命名空間屬性會包含在傳回的集合。Note that the namespace attribute is included in the returned collection.

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);  
Imports <xmlns:aw="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = _  
            <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>  

        Dim attList = _  
            From att In xmlTree.DescendantsAndSelf.Attributes _  
            Select att  

        Console.WriteLine(xmlTree)  
        Console.WriteLine("-----")  

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

這個範例會產生下列輸出:This example produces the following output:

<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 XMLLINQ to XML,命名空間會顯示為屬性。Note that unlike some other XML programming interfaces, in LINQ to XMLLINQ to XML, namespaces are surfaced as attributes.

雖然 Visual Basic 使用者可以使用整合式的屬性軸,以擷取具有指定名稱的屬性集合中的項目,沒有任何整合的 Visual Basic 軸來擷取集合中的所有項目的所有屬性。Although Visual Basic users can use the integrated attribute axis to retrieve attributes with a specified name from a collection of elements, there is no integrated Visual Basic axis to retrieve all attributes of all elements in a collection.

這個方法會使用延後的執行。This method uses deferred execution.

另請參閱

Attributes(IEnumerable<XElement>, XName) Attributes(IEnumerable<XElement>, XName) Attributes(IEnumerable<XElement>, XName) Attributes(IEnumerable<XElement>, XName)

傳回來源集合中每個項目之屬性的已篩選集合。Returns a filtered collection of the attributes of every element in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<System::Xml::Linq::XAttribute ^> ^ Attributes(System::Collections::Generic::IEnumerable<System::Xml::Linq::XElement ^> ^ source, System::Xml::Linq::XName ^ name);
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);
static member Attributes : seq<System.Xml.Linq.XElement> * System.Xml.Linq.XName -> seq<System.Xml.Linq.XAttribute>
<Extension()>
Public Function Attributes (source As IEnumerable(Of XElement), name As XName) As IEnumerable(Of XAttribute)

參數

source
IEnumerable<XElement>

IEnumerable<T>XElement,其中包含來源集合。An IEnumerable<T> of XElement that contains the source collection.

name
XName XName XName XName

要比對的 XNameThe XName to match.

傳回

IEnumerable<T>XAttribute,其中包含來源集合中每個項目之屬性的已篩選集合。An IEnumerable<T> of XAttribute that contains a filtered collection of the attributes of every element in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

範例

下列範例會擷取元素的集合,在此情況下包括Child1Child2項目。The following example retrieves a collection of elements, which in this case includes the Child1 and Child2 elements. 然後它會擷取該子集合的所有屬性,名稱為Att1It then retrieves all attributes of that child collection with a name of Att1.

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);  
Dim xmlTree As XElement = _  
    <Root Att1="content1" Att2="content2">  
        <Child1 Att1="content3" Att2="content4">  
        </Child1>  
        <Child2 Att1="content5" Att2="content6">  
        </Child2>  
    </Root>  

Dim attList = From att In xmlTree.Elements.Attributes("Att1") _  
                          Select att  

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

這個範例會產生下列輸出:This example produces the following output:

Att1="content3"  
Att1="content5"  

備註

請注意,與一些其他的 XML 程式設計介面,在LINQ to XMLLINQ to XML,命名空間會顯示為屬性。Note that unlike some other XML programming interfaces, in LINQ to XMLLINQ to XML, namespaces are surfaced as attributes.

這個方法會使用延後的執行。This method uses deferred execution.

另請參閱

適用於