XElement.Attribute Method (XName)

 

Returns the XAttribute of this XElement that has the specified XName.

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

public XAttribute Attribute(
	XName name
)

Parameters

name
Type: System.Xml.Linq.XName

The XName of the XAttribute to get.

Return Value

Type: System.Xml.Linq.XAttribute

An XAttribute that has the specified XName; null if there is no attribute with the specified name.

Some return collections of elements or attributes. This method returns only a single attribute. Sometimes this is referred to as a singleton (in contrast to a collection).

Visual Basic users can use the integrated attribute axis to retrieve the value of an attribute with a specified name.

The following example creates an element with an attribute. It then retrieves the attribute using this method.


                XElement xmlTree = new XElement("Root",
    new XAttribute("Att", "attribute content")
);
XAttribute att = xmlTree.Attribute("Att");
Console.WriteLine(att);

This example produces the following output:


Att="attribute content"

The following is the same example, but in this case the XML is in a namespace. For more information, see Working with XML Namespaces.


                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 + "Att", "attribute content")
);
XAttribute att = xmlTree.Attribute(aw + "Att");
Console.WriteLine(att);

This example produces the following output:


aw:Att="attribute content"

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: