XElement.Attributes Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a collection of attributes of this element.

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

Syntax

'Declaration
Public Function Attributes As IEnumerable(Of XAttribute)
public IEnumerable<XAttribute> Attributes()

Return Value

Type: System.Collections.Generic.IEnumerable<XAttribute>
An IEnumerable<T> of XAttribute of attributes of this element.

Remarks

The attributes in the returned collection are in the order that they were added to the element. If the XML tree was parsed from XML, the attributes are returned in document order.

This method uses deferred execution.

Examples

The following example creates an element with two attributes. It then uses this axis method to retrieve all attributes of the element.

Dim output As New StringBuilder
Dim xmlTree As XElement = <Root Att1="content1" Att2="content2"/>

Dim attList As IEnumerable(Of XAttribute) = _
From at In xmlTree.Attributes() _
Select at

For Each att In attList
    output.Append(att)
    output.Append(Environment.NewLine)
Next


OutputTextBlock.Text = output.ToString()
StringBuilder output = new StringBuilder();
XElement xmlTree = new XElement("Root",
    new XAttribute("Att1", "content1"),
    new XAttribute("Att2", "content2")
);
IEnumerable<XAttribute> attList =
    from at in xmlTree.Attributes()
    select at;
foreach (XAttribute att in attList)
    output.Append(att + Environment.NewLine);

OutputTextBlock.Text = output.ToString();

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.