使用linq到xml选择具有给定属性的元素

4 linq-to-xml

我有以下XML结构:

<artists>
    <artist> 
        <name></name> 
        <image size="small"></image> 
        <image size="big"></image> 
    </artist>
</artists>
Run Code Online (Sandbox Code Playgroud)

我需要选择具有给定属性的名称和图像(size = big).

var q = from c in feed.Descendants("artist")
        select new { name = c.Element("name").Value, 
                     imgUrl = c.Element("image").Value };
Run Code Online (Sandbox Code Playgroud)

如何在上面的查询中指定所需的图像属性(size = big)?

Mat*_*len 8

当你知道怎么做时,这很简单!

var artistsAndImage = from a in feed.Descendants("artist")
                      from img in a.Elements("image")
                      where img.Attribute("size").Value == "big"
                      select new { Name = a.Element("Name").Value
                                 , Image = img.Value};
Run Code Online (Sandbox Code Playgroud)

这将返回所有艺术家的所有名称和大图像.