Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through all XElement attributes and get their values

Tags:

c#

xml

xpath

How to loop through all XElement attributes and get their values?

foreach (SyndicationElementExtension extension in f.ElementExtensions)
{
    XElement element = extension.GetObject<XElement>();

    // How to loop through all its attributes and get their values?
}

Thank you!

like image 582
Friend Avatar asked May 10 '12 18:05

Friend


2 Answers

Simple - use the Attributes() method:

foreach (var attribute in element.Attributes())
{
    string value = attribute.Value;
    // ...
}
like image 136
Jon Skeet Avatar answered Oct 05 '22 23:10

Jon Skeet


Assuming you want an answer to this question

var img2 = feeds.Items
     .SelectMany(i => i.ElementExtensions
                       .Select(e => e.GetObject<XElement>().Attribute("url").Value)
                )
     .ToArray();
like image 20
L.B Avatar answered Oct 05 '22 22:10

L.B