2012-04-28 145 views
0

我怎樣才能獲得thumbanail獲取縮略圖,XML

如果您查看源代碼在這裏養活自己:

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

回答

7

你錯過了什麼的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 : "" 
       }; 
+0

這是行不通的。所有的縮略圖都是「」。你能解釋一下http://search.yahoo.com/mrss/的用途嗎?是任何有媒體的網站所需要的:x節點? – raklos 2012-04-28 16:35:31

+0

實際上忽略了 - 我的錯誤 – raklos 2012-04-28 16:43:55

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;