我如何获得缩略图
如果您在此处查看源Feed:
http://feeds.bbci.co.uk/news/world/middle_east/rss.xml
我尝试了以下内容,但最后一部分不适用于媒体:缩略图
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
};
答案 0 :(得分: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 : ""
};
答案 1 :(得分: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;