Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

c# - LINQ to XML attributes

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>

By default code:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it this way:

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

Here is Working Example Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...