Comments
Patrick Collands wrote: collands (AT) gmail com I'd be very grateful for an invitation. Thank you.
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..

SYS-CON.TV
Today's Top SOA Links


Using an RSS Feed as a Content Service in ASP.NET
Bind Web pages to XML just as to databases

In the case of our business problem we need to use RSS to provide up-to-date MSDN Events listings on any community Web site that will display them.

Creating RSS feeds is a bit more complex than consuming them (we'll get to creating an RSS 2.0 feed in ASP.NET shortly.) Luckily, the creation of RSS feeds has become very common. A quick visit to www.MSDNEvents.com shows that an RSS feed of upcoming events is available. Note the common orange "RSS Button" signifying a feed link (see Figure 1).

If we click on the button and take a quick look at the feed itself, we see the information contained in Listing 2. Note that in addition to information about the feed itself, each item has a title, a link, and a description.

Our ASP.NET code will display the event title, which comprises the City, State, and Date of the event, and will display that information as a link object whose target is the link URL from the RSS feed item.

In its simplest form, RSS processing is just XML processing. We could simply do the following:

  • Create a read a stream that is the RSS file.\
  • Load that stream into an XML DOM
  • Iterate the <item> elements in that DOM
  • Output the syndicated content anyway you choose, for instance:
    - Response. write HTML
    - Create a datasource object form the content and bind it to a display object such as a grid
    - Emit it through the render method of a control
Note that this design uses RSS as a simple service without doing any "local" persistence on the consuming Web server or using the publish date/time. We will simply poll the RSS file or feed each time the control is created.

This is one of the great powers of RSS, and it can be used very simply, but you can get far cleverer if you choose.

While we could write this code from scratch, there is a rich collection of RSS handling source code already in the public domain.

Our code will be based on Dariush Tasdighi's Code Project article on building a simple RSS3 library (www.codeproject.com/aspnet/Working_on_RSS_20.asp).

For a more advanced RSS control sample I suggest you check out Scott Mitchell's RSSFeed control and GotDotNet.com workspace at http://gotdotnet.com/Workspaces/Workspace.aspx? id=6a116a0f-66c4-4f91-9e13-a0d00960b9ad.

Our ASP.NET application defines an RSSUtilities class within a namespace called JoeOnDotNet.RSS, which contains the GetRSSFeed method.

Our JoeOnDotNet namespace also defines classes for the Feed's Root object (RSSRoot), Channel object (RSSChannel), Images (RSSImage), Items (RSSItem), and a collection class to contain RSSItems (RSSItemCollection).

GetRSSFeed creates an XMLText-Reader object that is parametrically defined by a location enumerator, a feed address, and a type enumerator. That XMLTextReader is then used to populate a table in a dataset.

Once our RSS feed items are in a Dataset Table, we simple bind the data to the user interface object we want to use to display the data, in our case a datagrid.

See default.aspx and default.aspx.cs (source code is available by viewing the article online at http://dotnet.sys-con.com/read/issue/archives/, Vol: 3, iss: 9). Notice that most of the code drives the selection of the RSS feed to use and that the actual table displayed from the RSS feed is just a little code.

Our web page looks like Figure 2. By binding to a different RSS source the page displays a different set of items (see Figure 3). Figure 4 shows our sample application, which includes a drop-down list of different feed types to be used in our grid.

Notice that the first four read an .xml file form the file system or via a URL, but the second two select a URL that points to one of our ASPX files.

Since we already have our RSS namespace with all of the classes we need to work with, an RSS feed, and a collection of RSS items, it will be easy for us to create an RSS item collection manually as it was to populate a collection from a feed file.

We simply need to create an RSSRoot object (see create_rss2_sample1.aspx), which contains Chanel, Images, and Item Collections objects, and then add RSSItems to the RSSRoot.RSSItemCollection. We then use the PublishRSS method of the RSSUtilities class to return our RSS XML by way of an XMLTextWriter.

By now you're getting the picture. At its root RSS processing is an example of the power of XML. We have a known (RSS 2.0) schema and we've implemented a set of classes to retrieve XML that complies with the schema from any source (file, URL, etc.), and then iterates the XML according to the schema to create HTML content.

As long as the origin of the content, in our case MSDNEvents.com, observes the schema, our Web site will always pull the latest items from the content publisher and we will never have to edit the containing Web sites.

Thus we have a very simple and powerful content syndication service that uses RSS and ASP.NET. You're up and running, but you're just getting started. Moving forward you can explore the optional channel sub elements, using trackbacks, third-party enhancement modules, as well as writing your own custom extension modules.

Enjoy the power of federated content using RSS and ASP.NET, and keep reading .NET Developers Journal.

About Joe Stagner
Joe Stagner joined Microsoft in 2001 as a technical evangelist and is now a Developer Community Champion with the Microsoft MSDN Team. Joe?s technical career began at age 14 with a part-time job as a robotics control programmer, and has visited virtually every genre of technical vocation from Device Driver Developer to President and CEO of a publicly traded New York City consulting firm. Joe?s development experiences have allowed him to create commercial software applications across a wide diversity of technical platforms, from Mainframes through UNIX and Linux, to Microsoft technologies on the Intel and Mobile computing platforms. While Joe is generally interested in all computing technology, in recent years he has been particularly focused on highly performant, geoscalable Web application architectures, multiplatform interoperability, and writing secure code. www.JoeOn.NET

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE