Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Syndicating Your Web Site's Content With RSS And ASP.NET

Scott Mitchell
Expert Author
Published: 2005-07-20

WebProNews RSS Feed


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)
    {
       // 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 < RSS_ITEMS; i++)
       {
         // write out
         writer.WriteStartElement("item");

         // write out -level information
         writer.WriteElementString("title",
               articleData.Rows[i]["title"].ToString());
         writer.WriteElementString("link",
           String.Format("http://myWebSite.com/article.aspx?id={0}",
               articleData.Rows[i]["articleID"]));
       writer.WriteElementString("description",
           articleData.Rows[i]["synopsis"].ToString());
       writer.WriteElementString("author",
           articleData.Rows[i]["author"].ToString());
       // use DateTimeFormatInfo "r" to use RFC 1123
       // date formatting (same as RFC 822)
       writer.WriteElementString("pubDate",
         ((DateTime) articleData.Rows[i]["dateAdded"]).ToString("r"));

         // write out
         writer.WriteEndElement();
       }

       // write out
       writer.WriteEndElement();

       // write out
       writer.WriteEndElement();

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

       writer.Close();
    }

    // write out the cached value
    Response.Write(Cache["RssFeed"].ToString());


    [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

    Receive Our Daily Email of Breaking eBusiness News


    About the Author:
    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

    WebProNews RSS Feed

    More Articles

    Contact WebProNews
    Advertisement





    TOP NEWS

    Targeted Information for Business
    WebProNews is part of the iEntry network

    Internet Business: Marketing: Small Business:
    WebProNews MarketingNewz SmallBusinessNewz
    WebProWorld AdvertisingDay PromoteNews
    EcommNewz SalesNewz EntrepreneurNewz

    Software: Search Engines: Web Design:
    WebMasterFree Jayde B2B DesignNewz
    NetworkingFiles SearchZA FlashNewz
    SecurityConfig SearchNewz WebSiteNotes

    Developer: IT Management: Security:
    DevWebPro ITManagement SecurityProNews
    DevNewz SysAdminNews SecurityConfig
    TheDevWeb NetworkingFiles NetworkNewz

    The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
    eBUSINESS RESOURCES






     Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
     WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
    About WebProNews
    WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

    WebProNews provides real-time coverage of internet business.

    Free Email Newsletters:
    WebProNews SearchNewz
    WebProWorld DevWebPro
    Marketing SecurityNews
    Plus over 100 other newsletters!

    Send me relevant info on products and services.


    WebProWorld
    Ten most recent posts.

    NetworkingFiles
    Featured Software

    WebProNews in the News
    View all recent mentions of WebProNews from around the world!

    Recent Articles On ...
    Google eBusiness
    Yahoo Ask Jeeves
    MSN Blogs
    Search Engines Blogging
    Affiliate Programs Marketing
    eCommerce Advertising
    eBay Sun Microsystems
    AOL Adsense
    Microsoft Adwords
    Oracle IBM
    Amazon Apple
    SEM Mac
    SEO iPod
    Adsense XBox
    PR Adobe



    iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us