如何遍历所有XElement属性并获取其值

时间:2012-05-10 18:25:57

标签: c# xml xpath

如何遍历所有XElement属性并获取其值?

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

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

谢谢!

2 个答案:

答案 0 :(得分:10)

简单 - 使用Attributes()方法:

foreach (var attribute in element.Attributes())
{
    string value = attribute.Value;
    // ...
}

答案 1 :(得分:3)

假设您想要this question

的答案
var img2 = feeds.Items
     .SelectMany(i => i.ElementExtensions
                       .Select(e => e.GetObject<XElement>().Attribute("url").Value)
                )
     .ToArray();