Extensions.Attributes Method (IEnumerable(Of 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)

<ExtensionAttribute>
Public Shared Function Attributes (
	source As IEnumerable(Of XElement)
) As IEnumerable(Of XAttribute)

Parameters

source
Type: System.Collections.Generic.IEnumerable(Of XElement)

An IEnumerable(Of T) of XElement that contains the source collection.

Return Value

Type: System.Collections.Generic.IEnumerable(Of XAttribute)

An IEnumerable(Of T) of XAttribute that contains the attributes of every element in the source collection.

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.


                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"

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.


                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"

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: