About a year ago I wrote an article titled, Syndicating Your Web Site's Content with RSS. In 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 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 SlashDot.org) syndicate their content using RSS 1.0. Others (like Yahoo! News provides 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 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 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: 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.WriteStartElement("rss");
  
writer.WriteAttributeString("version", "2.0");
  
// write out
  
writer.WriteStartElement("channel");
  
// write out -level elements
  
writer.WriteElementString("title", "Example RSS Feed Title");
  
writer.WriteElementString("link", "http://myWebSite.com/");
  
writer.WriteElementString("description",
          
"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
[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 RssFeed, a free, open-source custom server control I developed for displaying RSS syndication feeds.
Happy Programming!
*Originally published at Scott Mitchell, author of five ASP/ASP.NET books and founder of
Suggest a Correction
Found an error or have a suggestion? Let us know and we'll review it.





No comments yet. Be the first to comment!