加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:19:30 所属栏目:百科 来源:网络整理
导读:我有以下 XML结构: artists artist name/name image size="small"/image image size="big"/image /artist/artists 我需要选择具有给定属性的名称和图像(size = big). var q = from c in feed.Descendants("artist") select new { name = c.Element("name").
我有以下 XML结构:

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

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

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

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

解决方法

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读