I have been part of the team working on the latest version ASPNET RssToolkit, originally created by Dmitry Robsman. We enhanced this awesome Toolkit and have just released version 2.0 of the Toolkit. Please check out Codeplex to download this toolkit.

Here are some of the changes in this version –

  • Consume and publish Atom/Rdf/Opml feeds.
  • Strongly typed classes, close to Rss specification.
  • Feeds Aggregation using OPML. The outlines in OPML can have Rss/Atom/Rdf.
  • Rss Schema validation during aggregation.
  • Download Manager can be used to download any Url and return a Stream. It also has inbuilt caching mechanism. 
  • Added support for Extensions and Enclosures.
  • Added Visual Studio Testing Framework Unit Tests.
  • Rearranged Namespaces to closely match the features.

Here is how to use the RssToolkit. I have taken excerpts from ScottGu’s review on the original RssToolkit.

Consuming Feeds using RssToolkit -

A.      Using RssDataSource – This webcontrol gives you the ability to databind to an Rss/Atom/Rdf/Opml feed. It inherits from DataSourceControl to give itself the ability serve as a data source to data-bound controls. Here are the steps you can take to use RssDataSource in your page or user control –

1.       Add the RssDataSource and RssHyperLink to the Toolbox in Visual Studio.

Toolbox 

2.       In your Web form, drop the RssDataSource.

3.       Click the control and “Configure Data Source”

Configure RssDataSource 

4.       It ask you for the feed Url. The RssDataSourceConfigForm.cs is used in this case.

You can provide RSS/ATOM/RDF/OPML and RssDataSource will automatically identify the feed type and provide Databinding capability.

As you can see below, I’m actually providing an Atom feed URL to the RssDataSource.

RssDataSourceConfigForm 

5.       You can then bind a control like Gridview to this DataSource.

Configure Grid to use RssDataSource 

6.       I will “Edit Columns” on the GridView and have 2 columns, as shown –

Grid Columns for RssDataSource 

7.       You can also specify the number of items you want to be returned back. I will go in the code behind and change that to 5

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<%@ Register Assembly="RssToolkit" Namespace="RssToolkit.Web.WebControls" TagPrefix="cc1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Consuming Google News using RssToolkit</title>

</head>

<body>

    <form id="form1" runat="server">

    Google News (Atom Format)

    <div>

        <cc1:rssdatasource id="RssDataSource1" runat="server" maxitems="5" url="http://news.google.com/?output=atom"></cc1:rssdatasource>

    </div>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="RssDataSource1">

            <Columns>

                <asp:HyperLinkField DataNavigateUrlFields="link" DataTextField="link" HeaderText="Link"/>

                <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />

            </Columns>

        </asp:GridView>

    </form>

</body>

</html>

 

8.       Compile and View the page in browser –

RssDataSource Output 

B.      Consume using RssDocument.cs – RssDocument.cs class can be used from your code if you want to consume/publish or manipulate feeds. It provides various overloads for Loading a feed –

RssDocument Class 

·         Load - takes 3 overloads System.Uri, Xml string and XmlReader. You can provide Rss/Atom/Rdf/Opml type of formats for these 3 overloads and it will automatically identify the base feed type and construct the class.

·         SelectItems – this method returns IEnumerable which gives the ability to iterate over the collection. It is also used internally to provide data binding capability to RssDataSource.

·         ToXml – this method which returns an Xml as a string, provides the capability of publishing the feed into Rss/Atom/Rdf/Opml formats. The input parameter (DocumentType enum) suggests the format of Xml returned by this method.

·         ToDataSet – this method returns the feed as a DataSet.

Here is how to use the RssDocument –

void Page_Load(object sender, EventArgs e)

 {

    RssToolkit.Rss.RssDocument rss = RssToolkit.Rss.RssDocument.Load(new System.Uri("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"));

    Image1.ImageUrl = rss.Channel.Image.Url;

    GridView1.DataSource = rss.SelectItems();

    GridView1.DataBind();

 }

C.      Consume using Custom Generated Class and RssDl.exe – Let’s say you want to consume a feed which has some extensions that is not specified in the Rss specification such as Media Rss by Yahoo.
An example of Media Rss tags are -
<media:player url="http://youtube.com/?v=dMH0bHeiRNg" />
<media:thumbnail url="http://sjc-static6.sjc.youtube.com/vi/dMH0bHeiRNg/2.jpg" width="120" height="90" />
<media:title>Evolution of Dance</media:title>
<media:category label="Tags">Dancing comedy</media:category>
<media:credit>judsonlaipply</media:credit>


You can still get strong typing for these extensions by generating a custom class using the RssDl.exe

 

Usage – RssDl.exe “URL” “ClassName.cs”

E.g.

RssDl.exe “http://youtube.com/rss/global/top_favorites.rss” “YouTube.cs”

RssDl.exe “http://news.google.com/?output=rss” “GoogleNews.vb”

  As shown below I created a custom class for YouTube’s Top favorites feed, which has Media Rss tags.

RssDl 

                If you open the newly created class “YouTube.cs” you will see the extensions as Strongly typed properties inside the class.

    [XmlElement("player", Namespace="http://search.yahoo.com/mrss/")]

    public YoutubePlayer Player {

        get {

            return _player;

        }

        set {

            _player = value;

        }

    }

   

    [XmlElement("thumbnail", Namespace="http://search.yahoo.com/mrss/")]

    public YoutubeThumbnail Thumbnail {

        get {

            return _thumbnail;

        }

        set {

            _thumbnail = value;

        }

    }

   

    [XmlElement("category", Namespace="http://search.yahoo.com/mrss/")]

    public YoutubeCategory Category {

        get {

            return _category;

        }

        set {

            _category = value;

        }

    }

   

    [XmlElement("credit", Namespace="http://search.yahoo.com/mrss/")]

    public string Credit {

        get {

            return _credit;

        }

        set {

            _credit = value;

        }

    }

 

 

You will have to embed this class in your application or put in App_Code folder of your website. You can then use the class just like RssDocument.cs.

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Youtube Media Rss</title>

 

    <script language="c#" runat="server">

        void Page_Load(object sender, EventArgs e)

        {

            YoutubeRss rss = YoutubeRss.Load();

            DataList1.DataSource = rss.Channel.Items;

            DataList1.DataBind();

        }

    </script>

 

</head>

<body>

    <form id="form1" runat="server">

        You Tube Top Favories(Media Rss)

        <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" GridLines="both">

            <ItemTemplate>

                <asp:Image ID="Image1" runat="Server" ImageUrl='<%# Eval("Thumbnail.Url")  %>' /><br />

                        <asp:HyperLink ID="HyperLink1" NavigateUrl='<%# Eval("Player.Url") %>' Text='Link' runat="server" />

            </ItemTemplate>

        </asp:DataList>

    </form>

</body>

</html>

 

Here is what the page looks like -

Consuming Media Rss using RssToolkit 

Aggregation

RssToolkit provides Aggregation using Outline Processor Markup Language (OPML). Check out the OPML specification here. OPML is used for creating a list of feeds known as Outlines to be shared or aggregated. Here is what an OPML format looks like –

<?xml version="1.0" encoding="iso-8859-1"?>

<opml version="1.1">

  <head>

    <title>List of Feeds News Feeds</title>

    <dateCreated>Tue, 11 Nov 2003 19:47:24 GMT</dateCreated>

    <dateModified>Tue, 11 Nov 2003 19:47:28 GMT</dateModified>

    <ownerName>Webmaster</ownerName>

    <ownerEmail>owner@msdn.com</ownerEmail>

    <expansionState></expansionState>

    <vertScrollState>3</vertScrollState>

    <windowTop>93</windowTop>

    <windowLeft>127</windowLeft>

    <windowBottom>585</windowBottom>

    <windowRight>710</windowRight>

  </head>

  <body>

    outline text='BBC News’ description='Updated every minute of every day - FOR PERSONAL USE ONLY' htmlUrl='http://news.bbc.co.uk/go/click/rss/0.91/public/-/1/hi/default.stm' language='unknown' title='BBC News' type='rss' version='RSS' xmlUrl='http://news.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss091.xml'/>

    <outline text='CNET News.com' description='Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media.' htmlUrl='http://news.com.com/' language='unknown' title='CNET News.com' type='rss' version='RSS2' xmlUrl='http://news.com.com/2547-1_3-0-5.xml'/>

  </body>

</opml>

 

As seen above the Outline has the information of the feeds to be aggregated, XmlUrl provides the Url of the Feed.

To aggregate Feeds using RssToolkit you need to provide the location of the OPML file or proved the OPML Xml. You can use the Load method in the RssDocument.cs Class for Aggregation. As shown before the Load of RssDocument has 3 overloads –

public static RssDocument Load(string xml);
public static RssDocument Load(Uri url);
public static RssDocument Load(XmlReader reader);

 

Example -

RssDocument rssAggregatedFeed = RssDocument.Load (new System.Uri("http://static2.podcatch.com/blogs/gems/opml/mySubscriptions.opml"));

Publishing your Feeds

 

RssHyperLink – This WebControl can be used for publishing your Feed. You can point this tool to the feed location and it automatically generates link tag -

<link rel="alternate" type="application/rss+xml" title="Rss" href="~/RssHyperLink.ashx" />

 

This tag enables Feed Autodiscovery in browsers. It enables the Rss Autodiscovery in IE and FireFox. To publish the feeds using RssToolkit, you can use one of the mechanisms below –

 

A.       Using ASP.Net Buildprovider mechanism – RssToolkit uses ASP.Net BuildProvider class to automatically detect certain files created or modified in the filesystem. It automatically generates code and compiles the code for any files with extension *.rss or *.rssdl. RssToolkit then uses RssDocument.cs class and De-serializes the feed. It also builds an HttpHandler, which you can then inherit from to publish your feed.

In the samples provided. The App_Code folder has Sample5.Rss

<?xml version="1.0" encoding="utf-8"?>

<rss version="2.0">

    <channel>

        <title>Sample Channel</title>

        <link>~/RssHyperlinkFromCustomClass.aspx</link>

        <description>Channel For Scenario5 in ASP.NET RSS Toolkit samples.</description>

        <ttl>10</ttl>

        <name></name>

        <user></user>

        <pubDate>Tue, 10 Apr 2007 23:01:10 GMT</pubDate>

        <lastBuildDate>Tue, 10 Apr 2007 23:01:10 GMT</lastBuildDate>

        <webMaster>webmaster@email.com</webMaster>

        <item>

            <title></title>

            <description></description>

            <link></link>

        </item>

        <item>

            <title></title>

            <description></description>

            <link></link>

        </item>

    </channel>

</rss>

When the file is created or modified RssToolkit automatically generates code and compiles it based on the schema of this file. It pre-fixes all the files with “Sample5”. So like RssDocument there will be Sample5Rss, RssChannel will have Sample5Channel and so on. It also creates a default HttpHanlder called Sample5HtppHandlerBase which inherits from RssHttpHandlerBase. You can create a custom *.ashx HttpHandler which inherits from Sample5HttpHandlerBase and you can publish the Sample5.rss

<%@ WebHandler Language="C#" Class="RssHyperLinkFromCustomClass" %>

 

using System;

using System.Collections.Generic;

using System.Web;

using RssToolkit.Rss;

 

public class RssHyperLinkFromCustomClass: Sample5HttpHandlerBase

{

    protected override void PopulateRss(string rssName, string userName)

    {

        Rss.Channel = new Sample5Channel();

       

        Rss.Channel.Items = new List<Sample5Item>();

        if (!string.IsNullOrEmpty(rssName))

        {

            Rss.Channel.Title += " '" + rssName + "'";

        }

 

        if (!string.IsNullOrEmpty(userName))

        {

            Rss.Channel.Title += " (generated for " + userName + ")";

        }

 

        Rss.Channel.Link = "~/scenario6.aspx";

        Rss.Channel.Description = "Channel For Scenario6 in ASP.NET RSS Toolkit samples.";

        Rss.Channel.Ttl = "10";

        Rss.Channel.User = userName;

 

 

        Sample5Item item = new Sample5Item();

        item.Title = "CodeGeneratedClass";

        item.Description = "Consuming RSS feed programmatically using strongly typed classes";

        item.Link = "~/CodeGeneratedClass.aspx";

        Rss.Channel.Items.Add(item);

 

        item = new Sample5Item();

        item.Title = "ObjectDataSource";

        item.Description = "Consuming RSS feed using ObjectDataSource";

        item.Link = "~/ObjectDataSource.aspx";

        Rss.Channel.Items.Add(item);

    }   

}

You can use RssHyperLink to point to this Custom HttpHandler –

<cc1:RssHyperLink ID="Rsshyperlink4" runat="server" IncludeUserName="True" NavigateUrl="~/RssHyperLinkFromCustomClass.ashx">RSS</cc1:RssHyperLink>

When clicked on the link the output shown will be –

RssHandler output 

B.      Using RssDocument class – RssDocument.cs has a method ToXml(), which can be used to serialize and publish the RssDocument class.

public string ToXml(DocumentType outputType);

 

It takes DocumentType enum which has the following values –

public enum DocumentType
{
    Unknown,
    Rss,
    Opml,
    Atom,
    Rdf

}

 

So to publish your feed as an ATOM from RssDocument, you can do the following inside an ASP.Net Module or an HttpHandler –

string inputXml = rssDocument.ToXml(DocumentType.Atom); // Publish as Atom

XmlDocument document = new XmlDocument();
document.LoadXml(inputXml);

            context.Response.ContentType = "text/xml";

// save XML into response

            document.Save(HttpContent.Current.Response.OutputStream);

 

In the samples provided with the Toolkit you can see 2 samples which publish your feed into various formats. The output is as shown

output type as atom

As seen in the above sample, the output feed is picked up from the querystring “outputtype”. You can provide “rss”, “rdf”, “atom” and “opml” for the respective feeds.

Strongly typed classes –

The toolkit provides strong typing to the Rss Classes.

e.g.

void Page_Load(object sender, EventArgs e)

 {

    RssToolkit.Rss.RssDocument rss = RssToolkit.Rss.RssDocument.Load(new System.Uri("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"));

    Image1.ImageUrl = rss.Channel.Image.Url;

    GridView1.DataSource = rss.SelectItems();

    GridView1.DataBind();

 }

 

Caching of Feeds –

DownloadManager.cs class is used to get information of the feed. The feed is cached locally to the disk. The location and time of the caching is configurable. You can use the keys in the web.config to change the values –

<appSettings>

        <add key="defaultRssTtl" value="10"/>

        <add key="rssTempDir" value="c:\temp"/>

</appSettings>

 

You can download the ASP.Net RSSToolkit here.

You can Discuss or Post issues on Codeplex.

 Special Thanks to Dmitry Robsman, Marc Brooks and Jon Gallant.