How to retrieve a collection of attributes (LINQ to XML)
This article shows the use of the Attributes method to retrieve the attributes of an element.
Example: Iterate through the attributes of an element
The following example shows how to iterate through the collection of attributes of an element.
XElement val = new XElement("Value",
new XAttribute("ID", "1243"),
new XAttribute("Type", "int"),
new XAttribute("ConvertableTo", "double"),
"100");
IEnumerable<XAttribute> listOfAttributes =
from att in val.Attributes()
select att;
foreach (XAttribute a in listOfAttributes)
Console.WriteLine(a);
Dim val = _
<Value ID="1243" Type="int" ConvertableTo="double">100</Value>
Dim listOfAttributes As IEnumerable(Of XAttribute) = _
From att In val.Attributes() _
Select att
For Each att As XAttribute In listOfAttributes
Console.WriteLine(att)
Next
This example produces the following output:
ID="1243"
Type="int"
ConvertableTo="double"
See also
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
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