Windows Azure Blog
Microsoft Cloud Computing Platform
Microsoft Store
  • Home
  • Windows Azure Team Blog
You are here : Windows Azure Blog » Wade Wegner » Aggregating RSS Feeds in C# and ASP.NET MVC 3

Aggregating RSS Feeds in C# and ASP.NET MVC 3

Posted On Tuesday, December 27, 2011 By rss. Under Wade Wegner    

I’m working on a Windows Phone project that requires me to surface up multiple RSS feeds as a single source. I needed a way to do this quickly and easily, and with a little help from friends on Twitter (particularly a suggestion from @bertcraven) I found a nice way to accomplish this using the SyndicationFeed in System.ServiceModel.Syndication.

I’ve detailed the steps below, but if you want to get to the heart of it then here’s the code to get this working:

SyndicationFeed mainFeed = new SyndicationFeed();
List<string> feeds = new List<string>();

feeds.Add("http://feeds2.feedburner.com/WadeWegner");
feeds.Add("http://www.nickharris.net/feed/");
feeds.Add("http://feeds.feedburner.com/ntotten");
feeds.Add("http://michaelwasham.com/feed/");
feeds.Add("http://blogs.msdn.com/b/hpctrekker/rss.aspx");

foreach (var feed in GetRssFeeds())
{
    Uri feedUri = new Uri(feed);
    SyndicationFeed syndicationFeed;
    using (XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
    {
        syndicationFeed = SyndicationFeed.Load(reader);
    }

    syndicationFeed.Id = feed;

    SyndicationFeed tempFeed = new SyndicationFeed(
        mainFeed.Items.Union(syndicationFeed.Items).OrderByDescending(u => u.PublishDate));
    mainFeed = tempFeed;
}

It’s really quite simple – once you know how to do it!

As you iterate through the list of feeds we use LINQ to union the feeds together – in the end this produces a main feed that has all the contents. Along the way we sort the elements in a descending order based on the PublishDate – otherwise you’ll just get blocks from each of the feeds and nothing is sorted according to the date publish. Once this is done you end up with a main feed that you can use.

For me I wanted to create a service that published the aggregated feed – I chose to use ASP.NET MVC 3 for this new feed. Here are steps you can follow in order to get this working in ASP.NET MVC 3.

  1. Create a new ASP.NET MVC 3 Web Application. I’ve called mine RssFeed. 
    NewProject
  2. Choose an Internet Application using the Razor view engine and HTML5 semantic markup.
  3. Add System.ServiceModel as a reference in the application. We’ll use this with SyndicationFeed.
  4. Create an empty controller. I’ve called mine the RssController. 
    RssFeed
  5. We’re going to define our own ActionResult implementation that can emit RSS by deriving from ActionResult. Inspiration and original source comes from this post on Developer Zen.
    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }
    
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";
    
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }
  6. We can now update the Index method to use the RssActionResult instead of the default ActionResult implementation.
    public RssActionResult Index()
    {
        return new RssActionResult();
    }
  7. Define a method that returns all the feeds with which you want to aggregate. You can pull from many different places – I recommend SQL Azure – but for the purposes of this demo you can just use a generic list of strings.
    private static List<string> GetRssFeeds()
    {
        List<string> feeds = new List<string>();
    
        feeds.Add("http://feeds2.feedburner.com/WadeWegner");
        feeds.Add("http://www.nickharris.net/feed/");
        feeds.Add("http://feeds.feedburner.com/ntotten");
    
        return feeds;
    }
  8. Now we can update our Index method to iterate through the feeds and aggregate them into a single SyndicationFeed that is sorted (descending) by the publish date.
    public RssActionResult Index()
    {
        SyndicationFeed mainFeed = new SyndicationFeed();
    
        foreach (var feed in GetRssFeeds())
        {
            Uri feedUri = new Uri(feed);
            SyndicationFeed syndicationFeed;
            using (XmlReader reader = XmlReader.Create(feedUri.AbsoluteUri))
            {
                syndicationFeed = SyndicationFeed.Load(reader);
            }
    
            syndicationFeed.Id = feed;
    
            SyndicationFeed tempFeed = new SyndicationFeed(
                mainFeed.Items.Union(syndicationFeed.Items).OrderByDescending(u => u.PublishDate));
            mainFeed = tempFeed;
        }
    
        return new RssActionResult() { Feed = mainFeed };
    }
  9. Now, hit F5 and run. Browse to http://localhost:<port>/rss to see the aggregated RSS feed. 
    RssFeed

And that’s it!

There’s certainly more you can do with this – in fact, given the cost it takes to aggregate a large number of feeds, I’ve started to take the aggregated feed and store it in Windows Azure blob storage attached to the Content Delivery Network (CDN). The code to do this is similar to the following:

StringBuilder builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder))
{
    mainFeed.SaveAsRss20(writer);
    string rssFeed = builder.ToString();

}
// write to Windows Azure blob storage
You might consider doing something similar.
I hope this helps!

http://feedproxy.google.com/~r/WadeWegner/~3/RRDQbd0GB9c/

Share this:

  • Print
  • Email
  • Facebook
  • Twitter
  • Digg
  • Reddit
  • StumbleUpon
« Windows Azure Platform Training Kit – October Release
NuGet Packages for Windows Azure and Windows Phone Developers »
  • Categories
    • AppFabric Team Blog (13)
    • Channel 9 (440)
    • cloud development blog (42)
    • Cloudy in Seattle (10)
    • Convective (12)
    • Matias Woloski (15)
    • Nick Harris .NET – Enterprise Development with Azure, ASP .NET MVC and Windows Phone 7 (50)
    • OakLeaf Systems (427)
    • Scott Hanselman's Blog (29)
    • ScottGu (18)
    • SQL Azure Team Blog (29)
    • Stack Overflow Azure (7524)
    • Uncategorized (16)
    • Wade Wegner (19)
    • Windows Azure Developer Tools Team (25)
    • Windows Azure Marketplace DataMarket Blog (26)
    • Windows Azure Storage Team Blog (63)
    • Windows Azure Team Blog (563)
    • Windows Phone Developer Blog (56)
    • Zane Adam's blog (22)
  • Translator
    English flagItalian flagKorean flagChinese (Simplified) flagChinese (Traditional) flagPortuguese flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flagCroatian flagDanish flagFinnish flagHindi flagPolish flagRomanian flagSwedish flagNorwegian flagCatalan flagFilipino flagHebrew flagIndonesian flagLatvian flagLithuanian flagSerbian flagSlovak flagSlovenian flagUkrainian flagVietnamese flagAlbanian flagEstonian flagGalician flagMaltese flagThai flagTurkish flagHungarian flagBelarus flagIrish flagIcelandic flagMacedonian flagMalay flagPersian flag
  • Recent Posts
    • Create Azure Worker Role or Cloud Service in Code?
    • Windows Azure and Cloud Computing Posts for 6/17/2013+
    • Exclusive Sneak Peak: The AGENT Smart Watch Emulator and managed .NET code on my wrist!
    • Cross-Post: Microsoft Dynamics NAV and Microsoft Dynamics GP on Windows Azure Infrastructure Services!
    • Scrollbar appearing like in Azure management portal
  • Advertisements

  • RSS

    Windows Azure Blog

  • Twitter
  • Categories
    AppFabric Team Blog Channel 9 cloud development blog Cloudy in Seattle Convective Matias Woloski Nick Harris .NET - Enterprise Development with Azure, ASP .NET MVC and Windows Phone 7 OakLeaf Systems ScottGu Scott Hanselman's Blog SQL Azure Team Blog Stack Overflow Azure Uncategorized Wade Wegner Windows Azure Developer Tools Team Windows Azure Marketplace DataMarket Blog Windows Azure Storage Team Blog Windows Azure Team Blog Windows Phone Developer Blog Zane Adam's blog
Mocell WordPress Theme By MagPress.com
Thanks to Cat Lovers | Meet Locals | Florida Chat
Copyright © 2013. All Rights Reserved.
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.