使用linq to xml选择具有给定属性的元素 [英] Select element with given attribute using linq to xml

查看:70
本文介绍了使用linq to xml选择具有给定属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML结构:

<artists>
    <artist> 
        <name></name> 
        <image size="small"></image> 
        <image size="big"></image> 
    </artist>
</artists>

我需要选择具有给定属性(大小=大)的名称和图片.

I need to select name and image with given attribute (size = big).

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

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

how can I specify needed image attribute(size=big) in query above?

推荐答案

知道如何就很简单!

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};

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

This will return all the names and big images for all the artists.

这篇关于使用linq to xml选择具有给定属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆