1

我怎样才能得到缩略图

如果您在此处查看源提要:

http://feeds.bbci.co.uk/news/world/middle_east/rss.xml

我尝试了以下方法,但最后一部分不适用于媒体:thumbnail

 XDocument feedXML = XDocument.Load("http://feeds.bbci.co.uk/news/world/middle_east/rss.xml");
            var feeds = from feed in feedXML.Descendants("item")
                        select new
                        {
                            Title = feed.Element("title").Value,
                            Link = feed.Element("link").Value,
                            Description = feed.Element("description").Value,
                            pubDate = feed.Element("pubDate").Value,
                            guid = feed.Element("guid").Value,
                            thumbnail = feed.Element("media:thumbnail").Attribute("url").Value
                        };
4

2 回答 2

8

你错过的是 XNamespace + 一个空检查

XDocument feedXML = XDocument.Load("http://feeds.bbci.co.uk/news/world/middle_east/rss.xml");
XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");
var feeds = from feed in feedXML.Descendants("item")
                select new
                {
                    Title = feed.Element("title").Value,
                    Link = feed.Element("link").Value,
                    Description = feed.Element("description").Value,
                    pubDate = feed.Element("pubDate").Value,
                    guid = feed.Element("guid").Value,
                    thumbnail = feed.Element(media+"thumbnail")!=null ? feed.Element(media+"thumbnail").Attribute("url").Value : ""
                };
于 2012-04-28T14:27:58.293 回答
0

这是正确的:

XNamespace media = "http://search.yahoo.com/mrss/";
Title = feed.Element("title").Value;
Description = feed.Element("description").Value;
ThumbnailUrl = feed.Element(media + "thumbnail").Attribute("url").Value;
于 2014-01-03T18:39:46.197 回答