简体   繁体   中英

get thumbnail from rss feed linq, xml

How can i get the thumbanail

if you view the source feed its here:

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

I've tried the following but the last part wont work for media: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
                        };

What you miss is XNamespace + a null check

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

This would be correct:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM