Wednesday, September 18, 2024

Syndicating Your Web Site’s Content with RSS and ASP.NET

About a year ago I wrote an article titled, Syndicating Your Web Site’s Content with RSS.

In that article, I looked at how you can provide programmatic access to your site’s latest content through an XML-formatted syndication standard known as RSS. The code in Syndicating Your Web Site’s Content with RSS used classic ASP to generate the appropriately formatted RSS content. I decided that it was time to update the previous article, and illustrate how an RSS feed can be generated using ASP.NET!

The History of RSS

RSS is a standardized, XML-formatted means for syndication Web site content. The history of RSS can be traced back to the Resource Description Framework (RDF), a W3C standard first drafted in October 1997. The purpose of RDF, in a nutshell, was to provide a standard means representing information about resources on the Web, such as information like the title, author, the date published, a description, copyright notices, and other metadata for online articles, message board posts, FAQs, etc.

In 1999, Netscape created a watered-down version of the W3C’s RDF called RSS 0.9, or RDF Site Summary. RSS 0.9 adheres to RDF specification. Shortly afterwards the official RSS 1.0 is released. (The official specification for RSS 1.0 can be found at http://www.purl.org/rss/1.0/spec.) In 2000, Dave Winer introduces RSS 0.92, which breaks from the reliance on RDF, adding some new features. Dave changes the RSS acronym’s meaning to Really Simple Syndication. In August 2002, RSS 2.0 is released, an upgrade from RSS 0.92. (The official RSS 2.0 specification can be found at http://blogs.law.harvard.edu/tech/rss.)

The point is, RSS 1.0 and RSS 2.0 are different from one another. Some sites (like SlashDot.org) syndicate their content using RSS 1.0. Others (like MSDN’s latest headlines), uses RSS 2.0. Virtually all RSS aggregators can read both valid RSS 1.0 and RSS 2.0 feeds. Since I am more familiar with RSS 2.0, for this article, we’ll create an RSS syndication feed that adheres to the RSS 2.0 specification.

Examining the Structure of RSS 2.0

Understand that RSS is intended to provide information about a Web site’s latest content. For example, Yahoo! News provides RSS feeds of the latest news headlines. Here on 4Guys there is an RSS feed for the latest articles. Notice that RSS provides a synopsis for the recent articles or news items. It does not provide the complete content for all of the news items or articles. With this in mind, let’s look at the RSS format.

The root element of an RSS document is <rss>. It has precisely one child element, <channel>. The <channel> element contains information about the syndicated content, as well as information about each content item being syndicated. There are three required channel elements:

  • <title> – provides a title for the channel. For the 4Guys RSS feed this is: “4GuysFromRolla.com Headlines”
  • <link> – a URL to the channel (http://www.4guysfromrolla.com for the 4Guys feed)
  • <description> – a short description of the channel. For 4Guys this is: “Headlines for 4GuysFromRolla.com. 4Guys is an online resource site for ASP and ASP.NET information!”
  • There are additional optional elements, such as <language>, <copyright>, <webMaster>, and others. For a complete list refer to the RSS 2.0 specification.

    After those elements that describe the channel, there are a series of <item> elements, one for each content item being syndicated. Each <item> contains elements that describe the content item. Commonly, the <item> elements will contain the following four elements:

  • <title> – provides a title for the content item (for the 4Guys RSS feed, it’s the article’s name)
  • <link> – a URL to the content item
  • <description> – a synopsis of the content item
  • <pubDate> – the date the content item was published. (The date/time format must adhere to RFC #822. An example: Wed, 18 Feb 2004 15:18:01 GMT.)
  • Realize that only the <title> or the <description> element is required. In addition to the <title>, <link>, and <description> elements, there are additional optional elements, such as <author>, <category>, and <comments>, among others. For more information review the RSS 2.0 specification.

    To see an example RSS file, check out the 4Guys RSS feed, http://aspnet.4guysfromrolla.com/rss/rss.aspx.

    Syndicating Content Using ASP.NET

    To syndicate content as RSS, let’s create an ASP.NET Web page, RssFeed.aspx, that simply emits XML content. That is, this page is going to do nothing but return the corresponding XML output. There are a couple of ways to generate XML output. The cleanest approach is to use the XML classes in the .NET Framework (found in the System.Xml namespace). For this example, since we just want to write out a stream of XML, we’d want to use the XmlTextWriter class. If you’ve never worked with the XML classes, you might find it easier to use a Repeater Web control to emit the XML. In this article we’ll look at using the XmlTextWriter class; for an example of using a Repeater, refer to a previous article of mine: Creating an Online RSS News Aggregator with ASP.NET.

    An XmlTextWrtier works by writing XML content to some underlying Stream or TextWriter object. One option is to have the XML written directly to the Response object’s OutputStream. While this approach will definitely work, and can be made efficient using output caching, a more flexible approach would be to write the XML output to a string, so that this content could be cached using the ASP.NET Caching object. (The reason this approach would be more flexible is because the cached content can be cached for a specified duration, for a sliding time span, or based on some other dependency.) A thorough discussion on caching techniques is beyond the scope of this article, but for more information be sure to read Caching in ASP.NET.

    The following code demonstrates using the XmlTextWriter to syndicate content using RSS. Notice that this code assumes there is some DataTable, articleData, that contains the content to be syndicated. (This information will likely come from a database or some other persistent data store.) Also note that the ASP.NET page’s ContentType property has been set to text/xml and the ContentEncoding to UTF8.

    // Set the content-type
    Response.ContentType = "text/xml";
    Response.ContentEncoding = Encoding.UTF8;

    // check to see if a cached version exists
    if (Cache["RssFeed"] == null)
    {
    &nbsp&nbsp // build up the cache dynamically
    &nbsp&nbsp DataTable articleData = CreateDataSource();

    &nbsp&nbsp // Use an XmlTextWriter to write the XML data to a string...
    &nbsp&nbsp StringWriter sw = new StringWriter();
    &nbsp&nbsp XmlTextWriter writer = new XmlTextWriter(sw);

    &nbsp&nbsp // write out
    &nbsp&nbsp writer.WriteStartElement("rss");
    &nbsp&nbsp writer.WriteAttributeString("version", "2.0");

    &nbsp&nbsp // write out
    &nbsp&nbsp writer.WriteStartElement("channel");

    &nbsp&nbsp // write out -level elements
    &nbsp&nbsp writer.WriteElementString("title", "Example RSS Feed Title");
    &nbsp&nbsp writer.WriteElementString("link", "http://myWebSite.com/");
    &nbsp&nbsp writer.WriteElementString("description",
    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp "This is a demonstration RSS feed.");
    &nbsp&nbsp writer.WriteElementString("ttl", "60");

    &nbsp&nbsp // write out an element for each of the first X articles
    &nbsp&nbsp const int RSS_ITEMS = 10;
    &nbsp&nbsp for (int i = 0; i

    [View a Live Demo! | View the Complete Source Code]

    To use the above code in an ASP.NET Web page, place it in the Page_Load event handler. The HTML portion of the ASP.NET Web page should contain no markup. (The above code will also need the following namespaces imported: System.Data, System.IO, System.Xml, and System.Text.)

    Conclusion

    In this article we saw just how easy it was to create an ASP.NET Web page that syndicates Web site content using RSS. Once you have created such a page, others can consume your site's RSS feed through RSS aggregators. There are a bevy of aggregators, such as NewsGator, SharpReader, RssBandit, and FeedDemon, to name a few. You can also programmatically consume an RSS feed in an ASP.NET Web page. For more information on this, check out the article A Custom ASP.NET Server Control for Displaying RSS Feeds. In that article I examine RssFeed, a free, open-source custom server control I developed for displaying RSS syndication feeds.

    Happy Programming!

    *Originally published at 4GuysFromRolla.com

    Scott Mitchell, author of five ASP/ASP.NET books and founder of 4GuysFromRolla.com, has been working with Microsoft Web technologies for the past five years. An active member in the ASP and ASP.NET community, Scott is passionate about ASP and ASP.NET and enjoys helping others learn more about these exciting technologies. For more on the DataGrid, DataList, and Repeater controls, check out Scott's book ASP.NET Data Web Controls Kick Start (ISBN: 0672325012). Read his blog at : http://scottonwriting.net

    Related Articles

    1 COMMENT

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here

    Latest Articles