Wednesday, January 12, 2011

Reading an Rss feed with argotic

Findind an example on how to read a rss feed and access the encoded content was a pain.
Looks like the web is all about writing feed but not about reading one.

So here it is.

private void button1_Click(object sender, EventArgs e)
{
    string url  = @"http://feeds.feedburner.com/cenedella?format=xml";
    SyndicationResourceLoadSettings settings = new SyndicationResourceLoadSettings();                       
            
    RssFeed feed = RssFeed.Create(new Uri(url), settings);
                        
    foreach(var i in feed.Channel.Items) {

        var title   = i.Title;
        var date    = i.PublicationDate;
        var content = GetBlogPostContent(i, true);
    }
}
private string GetBlogPostContent(RssItem i, bool removeHtml){

    string content = null;
                
    foreach(ISyndicationExtension s in i.Extensions) {

        if(s is SiteSummaryContentSyndicationExtension){

            SiteSummaryContentSyndicationExtension ss = s as SiteSummaryContentSyndicationExtension;                    
            var t                                     = ss.Context.Encoded;
            content                                   = System.Web.HttpUtility.HtmlDecode(t);
            if(removeHtml)
                content = Regex.Replace(content, "<[^>]*>", "", RegexOptions.Compiled);
        }
    }
    return content;
}

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is good code, but something isn't panning out. I'm trying to parse feeds.feedburner.com/failblog which clearly has the correct node

    [content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"]

    but item.Extensions.Count() returns 0.

    Note: carats replaced with brackets so the code didn't get stripped out of the comment.

    ReplyDelete
  3. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Node JS Online Training from India .

    ReplyDelete