How to read a image url from a rss with syndicationFeed?

流过昼夜 提交于 2019-12-03 13:54:44

问题


How to get the image url? supposing that the tag is

    <media:thumbnail url="http://img.youtube.com/vi/y6_-cLWwEU0/default.jpg" width="120"
 height="90" />

using syndicationItem in syndicationFeed?

i have something like this

 Stream stream = e.Result;
                    XmlReader response = XmlReader.Create(stream);
                    SyndicationFeed feeds = SyndicationFeed.Load(response);
                    foreach (SyndicationItem ff in feeds.Items)
                    {

                        RssItem rssItem = new RssItem(ff.Title.Text, ff.Summary.Text, ff.PublishDate.ToString(), ff.Links[0].Uri.AbsoluteUri, **ff.image?????** );
                        rssItems.Add(rssItem);
                    }

any help??


回答1:


For example Google RSS keeps all images within a summury.

So u can extract it by this code:

List<RssFeedItem> rssItems = new List<RssFeedItem>();
                    Stream stream = e.Result;
                    XmlReader response = XmlReader.Create(stream);
                    SyndicationFeed feeds = SyndicationFeed.Load(response);
                    foreach (SyndicationItem f in feeds.Items)
                    {
                        RssFeedItem rssItem = new RssFeedItem();

                        rssItem.Description = f.Summary.Text;

 const string rx =  @"(?<=img\s+src\=[\x27\x22])(?<Url>[^\x27\x22]*)(?=[\x27\x22])"; 
                        foreach (Match m in Regex.Matches(f.Summary.Text, rx, RegexOptions.IgnoreCase | RegexOptions.Multiline))
                        {
                            string src = m.Groups[1].Value;
                            if (src.StartsWith("//")) // Google RSS has it
                            {
                                src = src.Replace("//", "http://");
                            }

                            rssItem.ImageLinks.Add(src);
                        }



回答2:


Stream stream = e.Result;

        XmlReader response = XmlReader.Create(stream);

        SyndicationFeed feeds = SyndicationFeed.Load(response);

        foreach (SyndicationItem item in feeds.Items)
        {
            if (item.ElementExtensions.Where(p => p.OuterName == "thumbnail").Count() != 0)
            {
                string imgPath = item.ElementExtensions.Where(p => p.OuterName == "thumbnail").First().GetObject<XElement>().Attribute("url").Value;
                MessageBox.Show(imgPath); //use it to show the img in DIV or whereever you wish.
            }

        }



回答3:


i also needed a solution and i used like this:

foreach (SyndicationItem item in feed.Items)
{
   int s,f;
        s = item.Summary.Text.IndexOf("<");
        f = item.Summary.Text.IndexOf("/>");

        if (f != -1)
            div1.InnerHtml += "</br>photo:" + item.Summary.Text.Substring(s, f + 1 - s);
}

I extract the img from the summary;



来源:https://stackoverflow.com/questions/7929314/how-to-read-a-image-url-from-a-rss-with-syndicationfeed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!