LINQ to XML属性 [英] LINQ to XML attributes

查看:60
本文介绍了LINQ to XML属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个简单的应用程序,该应用程序可以下载XML并进行解析.我对此有疑问:

I'm creating a simple application that download XML and parse it. I have a problem with these:

<image size="small">http://userserve-ak.last.fm/serve/34/101313093.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/101313093.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/101313093.jpg</image>
<image size="extralarge">http://userserve-ak.last.fm/serve/252/101313093.jpg</image>

默认代码:

var data = from query in xdoc.Descendants("user")
           select new User
           {
               Image = (string)query.Element("image")
           };

它总是将uri下载为小图片,但我想下载大图片. 怎么做?

It always download an uri to small image, but I want to download a large. How to do that?

推荐答案

您可以这样做:

var result= xdoc.Descendants("image")
                .Where(x => x.Attribute("size").Value == "large")
                .Select(x => new User{ Image =  x.Value });

这里是工作示例小提琴

这篇关于LINQ to XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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