When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, February 18, 2004

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

By Scott Mitchell


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!

- continued -

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 (https://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, https://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)
   // build up the cache dynamically
   DataTable articleData = CreateDataSource();

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

   // write out 
   writer.WriteAttributeString("version", "2.0");

   // write out 

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

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

      // write out -level information
      // use DateTimeFormatInfo "r" to use RFC 1123 
      // date formatting (same as RFC 822)
             ((DateTime) articleData.Rows[i]["dateAdded"]).ToString("r"));

      // write out 

   // write out 

   // write out 

   // save the string in the cache (cache for 1.5 hours)
   Cache.Insert("RssFeed", sw.ToString(), null, 
                   DateTime.Now.AddHours(1.5), TimeSpan.Zero);


// write out the cached value
[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.)


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.

Displaying Browser-Friendly RSS Feeds
When syndicating your site, if a user stumbles across the syndication feed through their web browser, they will see a jumble of XML content. This can be confusing to novice computer users or those who are not familiar with syndication concepts. You can alleviate this issue by using an XSL stylesheet to 'pretty up' your syndication feed's output when displayed through a browser. Be sure to read Displaying Browser-Friendly RSS Feeds for more information!

Happy Programming!

  • By Scott Mitchell


  • View the Complete Source Code

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article