Praneeth Puligundla
Praneeth Puligundla

Reputation: 415

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

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/

Upvotes: 1

Views: 1204

Answers (1)

Praneeth Puligundla
Praneeth Puligundla

Reputation: 415

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

}

Upvotes: 1

Related Questions