Freigeben über


ASP.Net RssToolkit Version 2.0

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="https://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="https://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("https://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="https://youtube.com/?v=dMH0bHeiRNg" />
<media:thumbnail url="https://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 “https://youtube.com/rss/global/top_favorites.rss” “YouTube.cs”

RssDl.exe “https://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="https://search.yahoo.com/mrss/")]

    public YoutubePlayer Player {

        get {

            return _player;

        }

        set {

            _player = value;

        }

    }

   

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

    public YoutubeThumbnail Thumbnail {

        get {

            return _thumbnail;

        }

        set {

            _thumbnail = value;

        }

    }

   

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

    public YoutubeCategory Category {

        get {

            return _category;

        }

        set {

            _category = value;

        }

    }

   

    [XmlElement("credit", Namespace="https://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="https://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='https://news.bbc.co.uk/go/click/rss/0.91/public/-/1/hi/default.stm' language='unknown' title='BBC News' type='rss' version='RSS' xmlUrl='https://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='https://news.com.com/' language='unknown' title='CNET News.com' type='rss' version='RSS2' xmlUrl='https://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("https://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("https://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.

Comments

  • Anonymous
    June 16, 2007
    PingBack from http://webdevelopment.gen4ik.com/2007/06/16/aspnet-rss-toolkit-20-released/

  • Anonymous
    June 17, 2007
    La version 2.0 de l'ASPNET RssToolkit est disponible sur Codeplex . Développée à l'origine par Dmitry

  • Anonymous
    June 18, 2007
    Piyush and IDisposable just released version 2.0 of the ASP.NET RSS Toolkit to CodePlex. New features

  • Anonymous
    June 18, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/06/18/aspnet-rss-toolkit-20-released/

  • Anonymous
    June 18, 2007
    ASP.NET RSS Toolkit v2.0 本月16号发布

  • Anonymous
    June 18, 2007
    PiyushandIDisposablejustreleasedversion2.0oftheASP.NETRSSToolkittoCodePlex.

  • Anonymous
    July 04, 2007
    Pierre et moi vous avions déjà parlé du RssToolkit pendant la tournée MixiMix en Septembre dernier. On

  • Anonymous
    July 05, 2007
    Pierre et moi vous avions déjà parlé du RssToolkit pendant la tournée MixiMix en Septembre dernier. On

  • Anonymous
    July 05, 2007
    PingBack from http://www.richiecarey.com/index.php/2007/07/05/links-for-2007-07-05/

  • Anonymous
    July 05, 2007
    Hi its good to see RSSToolkit, see how we can create RSS feeds http://geekswithblogs.net/narent/archive/2006/02/08/68647.aspx

  • Anonymous
    July 06, 2007
    本期共有10篇文章:

  • Anonymous
    July 07, 2007
    Thanks a lot very helpfull link

  • Anonymous
    July 09, 2007
    Scott Gu推荐的一些关于asp.net,asp.net ajax,visual studio,silverlight和IIS7的一些链接.

  • Anonymous
    July 10, 2007
    PingBack from http://thinkingindotnet.wordpress.com/2007/07/10/links-aspnet-aspnet-ajax-visual-studio-silverlight-e-iis7/

  • Anonymous
    July 10, 2007
    PingBack from http://alexjimenez.wordpress.com/2007/07/10/links-aspnet-aspnet-ajax-visual-studio-silverlight-e-iis7/

  • Anonymous
    July 15, 2007
    PingBack from http://blog.olivier-duval.info/articles/2007/07/15/quelques-mises-%C3%A0-jour-dokuwiki-asp-net-rsstoolkit

  • Anonymous
    July 15, 2007
    I came across this lovely toolkit if you are thinking consuming or publishing Rdf/Atom/Opml and Rss.

  • Anonymous
    July 22, 2007
    PingBack from http://aspdotnetpakistan.wordpress.com/2007/07/08/aspnet-rsstoolkit-2-releases/

  • Anonymous
    July 24, 2007
    I'vefallenbehindonmyweeklylink-listingseries-apologiesforthedelay. ASP.NET ...

  • Anonymous
    August 04, 2007
    ASP.NET

  • Anonymous
    August 23, 2007
    This toolkit is great. However, if you want to get a simple feed up and running quickly, RSS 2.0 is a pretty easy format. I found a great tutorial on how to create a feed with xmlbuilder at a site called codeislogic: <a href='http://asp-net.codeislogic.com/rss-20-feed-format-tutorial-for-asp-both-vbnet-or-csharpnet'>ASP  RSS Feed Format Tutorial</a>

  • Anonymous
    August 26, 2007
    This is superb! Great improvement - I've been waiting for Atom support :) Keep up the good work!

  • Anonymous
    August 27, 2007
    RssDl.exe “http://youtube.com/rss/global/top_favorites.rss” “YouTube.cs” gives me unsupported format, any idea?

  • Anonymous
    August 27, 2007
    Hi there, could you please explain some more what the RssSkipDays and RssSkipHours does for the RssChannel? Is it for consuming a feed and configuring the polling of the external feed? Or is it for building the channel to tell external aggregators to skip polling the source for certain days and hours? Cheers

  • Anonymous
    August 27, 2007
    An XML element that contains up to seven <day> sub-elements whose value is Monday - Sunday. Aggregators may not read the channel during days listed in the skipDays element. My bad it was just part of the spec

  • Anonymous
    September 02, 2007
    RssToolKit 2.0 でBlog最新記事の表示

  • Anonymous
    September 02, 2007
    RssToolKit 2.0 でBlog最新記事の表示

  • Anonymous
    September 15, 2007
    trust level="Medium" だと、RssToolKitは使えない

  • Anonymous
    September 15, 2007
    trust level="Medium" だと、RssToolKitは使えない

  • Anonymous
    September 15, 2007
    Released just two months, back... The new RSSToolkit by Microsft is a great facility to consume and publish

  • Anonymous
    September 17, 2007
    re: trust level=&quot;Medium&quot; だと、RssToolKitは使えない

  • Anonymous
    September 17, 2007
    re: trust level=&quot;Medium&quot; だと、RssToolKitは使えない

  • Anonymous
    September 17, 2007
    re: trust level=&quot;Medium&quot; だと、RssToolKitは使えない

  • Anonymous
    September 17, 2007
    trust level="Medium" だと、RssToolKitは使えない

  • Anonymous
    September 17, 2007
    re: trust level=&quot;Medium&quot; だと、RssToolKitは使えない

  • Anonymous
    September 17, 2007
    Hello, amazing toolkit. However if you consume Atom feeds, the link attribute will return a link to the main page, and not the actual link. Any plans to fix this soon? Thanks

  • Anonymous
    November 24, 2007
    Hi Piyush, I faced a problem while publishing RSS via RSSToolKit. It's an issue when i use a looping construct to Add Items to the Channel. Something like this: for (int i = 0; i < 4; i++)      {         item = new RssItem();         item.Title = "ObjectDataSource " + i.ToString();         item.Description = "Consuming RSS feed using ObjectDataSource " + i.ToString();         item.Link = "~/ObjectDataSource.aspx";         Rss.Channel.Items.Add(item);      } things look good in IE untill i subscribe. But when i subscribe to this feed, both IE7 reader, shows only 1 item under the feed. It should actually show 4 items, but it shows only one. Is there a specific way to use looping constructs while dealing with RSSToolkit ? Your help would be highly appreciated. Thanks, Ashwani

  • Anonymous
    December 14, 2007
    The comment has been removed

  • Anonymous
    December 18, 2007
    PingBack from http://dvermablog.wordpress.com/2007/12/19/aspnet/

  • Anonymous
    January 14, 2008
    Hey great toolkit! I have been able to use some of the features, but have been having difficulty with the "SelectItems" in the "RssDocument.cs" class. Can you show an example of how to use? Can I set this equal to a system.collections.generic and sort? Could you show an exmample of this?

  • Anonymous
    January 20, 2008
    PingBack from http://websitescripts.247blogging.info/piyush-shahs-blog-aspnet-rsstoolkit-version-20/

  • Anonymous
    January 28, 2008
    PingBack from http://www.csyangpeng.cn/post/2008/01/ASPNET-RSS-Toolkit.aspx

  • Anonymous
    February 27, 2008
    July 4th Links: ASP.NET, ASP.NET AJAX, Visual Studio, Silverlight and IIS7

  • Anonymous
    May 01, 2008
    May 1st, is the official "RSS Awareness Day". While we shouldn't forget all the celebrations ( International

  • Anonymous
    June 02, 2008
    &#160; ネタ元1: ASP.Net RssToolkit Version 2.0 ( Piyush Shah&#39;s Blog ) ネタ元2: Awesome ASP.NET 2.0 RSS

  • Anonymous
    June 22, 2008
    PingBack from http://hana.finestlovestories.com/blogasp.html

  • Anonymous
    July 29, 2008
    What is the difference between this toolkit and Windows RSS Plaftorm's?

  • Anonymous
    September 21, 2008
    nice toolkit, I was able to immediately plug it in and to pickup my blog feeds. thanks

  • Anonymous
    October 23, 2008
    I think we have to implement caching for RSS Data Source to boost the performance, anyone have did it ?

  • Anonymous
    May 01, 2009
    Hi Piyush, I am working with RSSToolkit to retrieve the description from a particular feed and save it in MS SQL Database. However, with the following piece of code, to retrieve the description, I get only a part of the description. rss = RssToolkit.Rss.RssDocument.Load(new System.Uri("http://www.bollywoodworld.com/news/rss.php")); foreach (RssItem item in rss.Channel.Items) {    list.Add(item.Description); } How can I retrieve the entire description and not just the part of it using RSSToolkit. Or is there any other work around that to retrieve the complete detail? Thanks, Darshana

  • Anonymous
    June 13, 2009
    Does anyone know if this is the state-of-the-art in RSS technology as of 6/2009?  I'm looking for the latest control I can add to my web page - so I don't have to update the code for a year or two. I've used the MOSS 2007 version of the RSS webpart, which is really nice and simple.  I've also copied what I think are the essential pieces of it.  But I'm not sure how to build a reusable control from it. Any suggestions? Thanks!

  • Anonymous
    August 18, 2009
    Hi I would like to know does the RSSDataSource grabs tags of blogpost from feeds? How am I able to extract it from the RSS feeds? Thanks for the help.

  • Anonymous
    March 02, 2010
    I have published feeds and they will be updated every 15min.How would i call ashx handler every 15min.

  • Anonymous
    January 05, 2011
    Hello.  Is there a 64-bit version of RssToolkit.dll or can I/you create one? Thanks, Scott