Instructor-led Online Courses in ASP.NET Core and Angular by Bipin Joshi. Read more...
Registration open for ASP.NET Core and Angular instructor-led online courses. Courses conducted by Bipin Joshi on weekends. Click here for more details.

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 a software consultant, trainer, author and yoga mentor having 22+ years of experience in software development. He also conducts online courses on ASP.NET MVC, ASP.NET Core and Design Patterns. 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 : Twitter  Facebook  Google+  LinkedIn

Posted On : 13 December 2011

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