Untitled 1

Aggregating RSS Feeds

.NET framework provides classes to read and write RSS and ATOM feeds. I already wrote two articles Using Syndication Classes to Generate RSS Feeds and Using Syndication Classes to Read RSS Feeds that illustrate use of these classes. These classes reside in System.ServiceModel.Syndication namespace and primarily work with one feed at a time. Sometimes, however, you need to aggregate multiple feeds to create a single feed. The code sample below shows you how to do accomplish this in an ASP.NET web form:

protected void Page_Load(object sender, EventArgs e)
    //read feed 1
    XmlReader reader1 = XmlReader.Create("enter URL to feed1");
    Rss20FeedFormatter formatter1 = new Rss20FeedFormatter();

    //read feed 2
    XmlReader reader2 = XmlReader.Create("enter URL to feed2");
    Rss20FeedFormatter formatter2 = new Rss20FeedFormatter();

    //merge and sort feed 1 and feed 2 items
    List<SyndicationItem> allItems = new List<SyndicationItem>();

    //final feed
    SyndicationFeed feed = new SyndicationFeed();
    feed.Title = new TextSyndicationContent("My RSS Feed");
    feed.Copyright = new TextSyndicationContent
("Copyright (C) 2011. All rights reserved.");
    feed.Description = new TextSyndicationContent
("RSS Feed Generated .NET Syndication Classes");
    feed.Generator = "My RSS Feed Generator";
    feed.Items = allItems;

    //write final feed
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.ContentType = "text/xml";
    XmlWriter rssWriter = XmlWriter.Create(Response.Output);
    Rss20FeedFormatter formatter3 = new Rss20FeedFormatter(feed);

public int CompareDates(SyndicationItem x, SyndicationItem y)
    return y.PublishDate.CompareTo(x.PublishDate);

The code essentially reads RSS feed data from two different feeds using XmlReader and Rss20FeedFormatter classes. It then merges the feed items and also sorts them as per publication date. Notice the use of CompareDates() method to sort the feed items. The list of SyndicationItem is then assigned to the Item property of SyndicationFeed. Finally, the aggregated feed items are written on the response stream.


Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 13 December 2011

Tags : ASP.NET Web Forms .NET Framework C#