How to retrieve a single attribute (LINQ to XML)
This article explains how to retrieve a single attribute of an element, given the attribute name. This is useful for writing query expressions where you want to find an element that has a particular attribute.
The Attribute method of the XElement class returns the XAttribute with the specified name.
Example: Retrieve attribute values, given the element and attribute names
The following example uses the XElement method to create an element named PhoneNumbers
. It then finds all the descendant elements named Phone
and, for each, uses the Attribute method to obtain and output the value of the attribute named type
:
XElement cust = new XElement("PhoneNumbers",
new XElement("Phone",
new XAttribute("type", "home"),
"555-555-5555"),
new XElement("Phone",
new XAttribute("type", "work"),
"555-555-6666")
);
IEnumerable<XElement> elList =
from el in cust.Descendants("Phone")
select el;
foreach (XElement el in elList)
Console.WriteLine((string)el.Attribute("type"));
Dim cust As XElement = <PhoneNumbers>
<Phone type="home">555-555-5555</Phone>
<Phone type="work">555-555-6666</Phone>
</PhoneNumbers>
Dim elList = From el In cust...<Phone>
For Each e As XElement In elList
Console.WriteLine(e.@type)
Next
This example produces the following output:
home
work
Example: Retrieve an attribute value with a cast
You can retrieve the value of an attribute by casting it, just as you do for with XElement objects. The following example demonstrates this:
XElement cust = new XElement("PhoneNumbers",
new XElement("Phone",
new XAttribute("type", "home"),
"555-555-5555"),
new XElement("Phone",
new XAttribute("type", "work"),
"555-555-6666")
);
IEnumerable<XElement> elList =
from el in cust.Descendants("Phone")
select el;
foreach (XElement el in elList)
Console.WriteLine((string)el.Attribute("type"));
Dim cust As XElement = <PhoneNumbers>
<Phone type="home">555-555-5555</Phone>
<Phone type="work">555-555-6666</Phone>
</PhoneNumbers>
Dim elList As IEnumerable(Of XElement) = _
From el In cust...<Phone> _
Select el
For Each el As XElement In elList
Console.WriteLine(el.@type)
Next
This example produces the following output:
home
work
LINQ to XML provides explicit cast operators for the XAttribute class to string
, bool
, bool?
, int
, int?
, uint
, uint?
, long
, long?
, ulong
, ulong?
, float
, float?
, double
, double?
, decimal
, decimal?
, DateTime
, DateTime?
, TimeSpan
, TimeSpan?
, GUID
, and GUID?
.
Example: Cast for an attribute in a namespace
The following example shows the same code for an attribute that's in a namespace. For more information, see Namespaces overview.
XNamespace aw = "http://www.adventure-works.com";
XElement cust = new XElement(aw + "PhoneNumbers",
new XElement(aw + "Phone",
new XAttribute(aw + "type", "home"),
"555-555-5555"),
new XElement(aw + "Phone",
new XAttribute(aw + "type", "work"),
"555-555-6666")
);
IEnumerable<XElement> elList =
from el in cust.Descendants(aw + "Phone")
select el;
foreach (XElement el in elList)
Console.WriteLine((string)el.Attribute(aw + "type"));
Imports <xmlns:aw="http://www.adventure-works.com">
Module Module1
Sub Main()
Dim cust As XElement = _
<aw:PhoneNumbers>
<aw:Phone aw:type="home">555-555-5555</aw:Phone>
<aw:Phone aw:type="work">555-555-6666</aw:Phone>
</aw:PhoneNumbers>
Dim elList As IEnumerable(Of XElement) = _
From el In cust...<aw:Phone> _
Select el
For Each el As XElement In elList
Console.WriteLine(el.@aw:type)
Next
End Sub
End Module
This example produces the following output:
home
work
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