How to retrieve the value of an attribute (LINQ to XML)
This article shows how to obtain the value of attributes. There are two main ways: You can cast an XAttribute to the desired type; the explicit conversion operator then converts the contents of the element or attribute to the specified type. Alternatively, you can use the Value property. However, casting is generally the better approach. If you cast the attribute to a nullable value type, the code is simpler to write when retrieving the value of an attribute that might or might not exist. For examples of this technique, see How to retrieve the value of an element.
Example: Use a cast to retrieve the value of an attribute
To retrieve the value of an attribute in C#, you cast the XAttribute object to your desired type. In Visual Basic, you can use the integrated attribute property to retrieve the value.
XElement root = new XElement("Root",
new XAttribute("Attr", "abcde")
);
Console.WriteLine(root);
string str = (string)root.Attribute("Attr");
Console.WriteLine(str);
Dim root As XElement = <Root Attr="abcde"/>
Console.WriteLine(root)
Dim str As String = root.@Attr
Console.WriteLine(str)
This example produces the following output:
<Root Attr="abcde" />
abcde
Example: Use a cast to retrieve from XML that's in a namespace
The following example shows how to retrieve the value of an attribute if the attribute is in a namespace. For more information, see Namespaces overview.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(aw + "Attr", "abcde")
);
string str = (string)root.Attribute(aw + "Attr");
Console.WriteLine(str);
Imports <xmlns:aw="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim root As XElement = <aw:Root aw:Attr="abcde"/>
Dim str As String = root.@aw:Attr
Console.WriteLine(str)
End Sub
End Module
This example produces the following output:
abcde
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for