XElement.Attribute(XName) XElement.Attribute(XName) XElement.Attribute(XName) XElement.Attribute(XName) Method


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

 System::Xml::Linq::XAttribute ^ Attribute(System::Xml::Linq::XName ^ name);
public System.Xml.Linq.XAttribute Attribute (System.Xml.Linq.XName name);
member this.Attribute : System.Xml.Linq.XName -> System.Xml.Linq.XAttribute
Public Function Attribute (name As XName) As XAttribute


XName XName XName XName

The XName of the XAttribute to get.


An XAttribute that has the specified XName; null if there is no attribute with the 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");  
Dim xmlTree As XElement = <Root Att="attribute content"/>  

Dim att As XAttribute = xmlTree.Attribute("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");  
Imports <xmlns:aw="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim xmlTree As XElement = <aw:Root aw:Att="attribute content"/>  

        Dim att As XAttribute = xmlTree.Attribute(GetXmlNamespace(aw) + "Att")  
    End Sub  
End Module  

This example produces the following output:

aw:Att="attribute content"  


Some axis methods 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.

Applies to

See also