This documentation is archived and is not being maintained.

Extensions.Attributes Method (IEnumerable<XElement>)

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

Namespace:  System.Xml.Linq
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)

public static IEnumerable<XAttribute> Attributes(
	this IEnumerable<XElement> source
)

Parameters

source
Type: System.Collections.Generic.IEnumerable<XElement>

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

Return Value

Type: System.Collections.Generic.IEnumerable<XAttribute>
An IEnumerable<T> of XAttribute that contains the attributes of every element in the source collection.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<XElement>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

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.

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.

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

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"

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0
Show: