How to separate img tags in description of xml (RSS FEED)

大城市里の小女人 提交于 2020-01-06 02:58:06

问题


I am unable to retrieve images from RSS feeds i.e., in description.

I am using the following code to retrieve information.

 var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")
                 orderby datetime(el.Element("pubDate").Value) descending
             select new
                 {
                     Title = el.Element("title").Value,
                     Link = el.Element("link").Value,
                     Description =el.Element("description").Value,
                     PubDate = datetime(el.Element("pubDate").Value),

                 };

When Description is being displayed, both text and image are being displayed togather I want to separate text and image in description. Can you please let me know how to proceed.

RSS Feed used : http://news.yahoo.com/rss/


回答1:


   var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")

                 orderby datetime(el.Element("pubDate").Value) descending

                 select new
                 {
                     Title = el.Element("title").Value,
                     Link = el.Element("link").Value,
                     Description =replace_other(el.Element("description").Value),
                     Image= regex(el.Element("description").Value),
                     PubDate = datetime(el.Element("pubDate").Value),

                 };

   lvFeed.DataSource = rssFeed;
   lvFeed.DataBind(); 

}

 protected string regex(string source)
  {
   var reg1 = new Regex("src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))   
       (?:\"|\')?");
   var match1 = reg1.Match(source);
   if (match1.Success)
   {
       Uri UrlImage = new Uri(match1.Groups["imgSrc"].Value, UriKind.Absolute);
       return UrlImage.ToString();
   }
   else
   {

       return null;
   }

}



来源:https://stackoverflow.com/questions/15649759/how-to-separate-img-tags-in-description-of-xml-rss-feed

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