Extensions.Attributes Method

Definition

Returns a collection of the attributes of every element in the source collection.

Overloads

Attributes(IEnumerable<XElement>)

Returns a collection of the attributes of every element in the source collection.

Attributes(IEnumerable<XElement>, XName)

Returns a filtered collection of the attributes of every element in the source collection. Only elements that have a matching XName are included in the collection.

Remarks

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

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

Returns a collection of the attributes of every element in the source collection.

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

Parameters

source
IEnumerable<XElement>

An IEnumerable<T> of XElement that contains the source collection.

Returns

An IEnumerable<T> of XAttribute that contains the attributes of every element in the source collection.

Examples

The following example retrieves a collection of elements, and then retrieves a collection of all attributes of all elements in the collection. 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.

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

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"  

The following is the same example, but in this case the XML is in a namespace. For more information, see Work with XML Namespaces. Note that the namespace attribute is included in the returned collection.

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

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"  

Remarks

Note that unlike some other XML programming interfaces, in LINQ to XML, namespaces are surfaced as attributes.

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.

See also

Applies to

.NET 10 i druge verzije
Proizvod Verzije
.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, 8, 9, 10
.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, 4.8.1
.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)

Source:
Extensions.cs
Source:
Extensions.cs
Source:
Extensions.cs

Returns a filtered collection of the attributes of every element in the source collection. Only elements that have a matching XName are included in the collection.

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

Parameters

source
IEnumerable<XElement>

An IEnumerable<T> of XElement that contains the source collection.

name
XName

The XName to match.

Returns

An IEnumerable<T> of XAttribute that contains a filtered collection of the attributes of every element in the source collection. Only elements that have a matching XName are included in the collection.

Examples

The following example retrieves a collection of elements, which in this case includes the Child1 and Child2 elements. It then retrieves all attributes of that child collection with a name of 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);  

This example produces the following output:

Att1="content3"  
Att1="content5"  

Remarks

Note that unlike some other XML programming interfaces, in LINQ to XML, namespaces are surfaced as attributes.

This method uses deferred execution.

See also

Applies to

.NET 10 i druge verzije
Proizvod Verzije
.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, 8, 9, 10
.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, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0