Steve C. Orr

Software Engineer, Web Developer, Database Designer
 
  

 











































































































































LANGUAGES: VB.NET | C#

ASP.NET VERSIONS: 2.x

Feed RSS to Your Users

Create RSS Feeds for Users to Consume on Their Terms

By Steve C. Orr

When you publish a new item on your Web site you usually must wait for users to visit it in order for them to discover the new item. And there’s no telling when that will be. Even if your users would be genuinely excited about the new item, they simply don’t know about it yet. Some Web sites send out e-mail newsletters to inform users of developing news, but, for a variety of reasons, this is often less than ideal: overzealous spam filters, overwhelmed inboxes, and information overload can cause users to overlook such an information source. Users get annoyed by e-mail newsletters that come too often, and newsletters that come too infrequently could cause users to lose interest in your site. What’s really needed is a technology that puts users in the driver’s seat. Most users have limited time and, therefore, prefer a quick and consolidated summary of what’s new and noteworthy on your site and others. That’s where RSS comes in ...

What Is RSS?

Really Simple Syndication (RSS) is an XML-based format that provides a simple way to let users know about new news stories in a way that’s convenient to them. There is a virtually infinite supply of RSS feeds available on the Internet. When a user visits an interesting Web site and sees a symbol similar to Figure 1, it’s an indication that an RSS feed is available to which they may want to subscribe.


Figure 1: This little symbol indicates to users that the Web site they are viewing has an RSS feed available so they can easily keep up to date on breaking news stories from that site.

There is a variety of software available on the Internet that lets users subscribe to and consume RSS feeds in a fashion that’s convenient to them. Such news aggregators keep track of which stories users have already read, and generally highlight new news stories that may be of interest. While there are software programs available that are dedicated solely to RSS reading and aggregation, RSS support is also built into most current Web browsers. Internet Explorer 7 has fantastic new RSS support built in (see Figure 2). Firefox has reasonably nice RSS support built in, as well.


Figure 2: Internet Explorer 7 has excellent new built-in RSS reading capabilities, including friendly subscription features, attractive RSS formatting, and powerful filtering capabilities.

RSS Format

For your Web site to provide RSS support, it must simply provide an XML file in a specific format that summarizes the news articles you wish to publicize. While the XML format for RSS has a long and controversial history, RSS 2.0 is the format I recommend, because it is simple and widely supported. A sample of this format is shown in Figure 3

<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0">

<channel>

  <title>Toothpaste and Ammunition web site</title>

  <link>http://www.T&A.net</link>

  <description>News about Toothpaste and Ammo</description>

  <language>en-US</language>

  <webMaster>BarryBigalow@T&A.net</webMaster>

  <item>

    <title>Colgate Supreme prevents tooth decay</title>

    <description>Preventing tooth decay</description>

    <link> http://www.T&A.net/decay.aspx</link>

    <author>MaryBigalow@T&A.net</author>

    <category>Toothpaste</category>

    <pubDate>Fri, 15 Jan 2007 03:47:37 GMT</pubDate>

  </item>

  <item>

    <title>.44 caliber special</title>

    <description>Big ammo sale</description>

    <link>http://www.T&A.net/sale.aspx</link>

    <author>TerryBigalow@T&A.net</author>

    <category>Ammo</category>

    <pubDate>Sat, 16 Jan 2007 04:48:21 GMT</pubDate>

  </item>

</channel>

</rss>

Figure 3: This XML sample shows the RSS 2.0 format, which is simple and widely supported.

An RSS feed contains a channel element, which contains the required sub elements title, link, and description. The channel can also contain other optional sub elements, such as language, webMaster, and others. Figure 4 contains a list of the channel element’s required elements, along with many of its more popular optional elements. For a complete list, see the RSS 2.0 specification at http://validator.w3.org/feed/docs/rss2.html.

Channel Elements

Description of the element’s value

title

(Required) The name of the channel as it should appear to end users.

description

(Required) A brief narrative about the contents of the channel.

link

(Required) The Url that leads to the web site that corresponds with this channel.

language

(Optional) Standard language code of the main language of the web site.

webMaster

(Optional) The email address of the web site’s web master.

lastBuildDate

(Optional) The time and date that the channel contents last changed.

ttl

(Optional) “Time To Live” – The number of minutes that the channel can be cached.

Figure 4: These are some of the most useful channel elements defined by the RSS 2.0 XML standard.

The channel element also contains a series of Items, which each also contain the required sub elements title, link, and description. It too can contain a variety of optional elements, such as the category and pubDate elements shown in this example. Figure 5 contains a list of all the required and optional item sub elements.

Item Elements

Description of the element’s value

title

(Required) The name of the item as it should appear to end users.

description

(Required) A brief narrative about the contents of the item.

link

(Required) The Url that leads to the full article.

author

(Optional) The email address of the item’s author.

category

(Optional) The name of the category to which the item belongs.

comments

(Optional) A Url to a page that has comments about the item.

enclosure

(Optional) The Url to a media file about the item (such as a hyperlink to an MP3 or video file.)

guid

(Optional) A string that uniquely identifies the item.

pubDate

(Optional) The date and time that the item was published

source

(Optional) The RSS channel from which the item originally came.

Figure 5: This is the complete list of sub elements supported by the item element according to the RSS 2.0 XML standard.

SiteMap Files

Many ASP.NET 2.0 Web applications already have an XML document that describes the pages that comprise the site. ASP.NET 2.0 SiteMaps have a structure not entirely dissimilar to the RSS 2.0 standard. All it takes is a little code to translate a SiteMap into the RSS 2.0 structure. Figure 6 shows a sample of a basic SiteMap file.

<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com..." >

  <siteMapNode url="" title="Top"  description="Top">

      <siteMapNode url=""

 title="Software"

                   description="Software">

          <siteMapNode url="~\default4.aspx"

                        title="Financial" 

                        description="Financial" />

          <siteMapNode url="~\default5.aspx"

title="Office Suites" 

                        description="Office Suites" />

      </siteMapNode>

      <siteMapNode url=""

 title="Hardware" 

 description="Hardware">

    <siteMapNode url="~\default8.aspx"

                        title="Computers" 

                        description="Computers" />

          <siteMapNode url="~\default9.aspx"

      title="Keyboards" 

      description="Keyboards" />

      </siteMapNode>

      <siteMapNode url=""

 title="Books" 

 description="Books">

          <siteMapNode url="~\defaultb.aspx"

      title="Fiction" 

      description="Fiction" />

          <siteMapNode url="~\default.aspx"

                        title="Finances" 

      description="Finances" />    

      </siteMapNode>

  </siteMapNode>

</siteMap>

Figure 6: ASP.NET 2.0 SiteMap files are comprised of a format that is not entirely dissimilar to the RSS 2.0 XML standard, which makes it easy to translate from one format to the other.

As you can see, each SiteMap file contains title, description, and url attributes — very similar to the item elements in an RSS 2.0 file. SiteMap files can be very useful for binding to menus and other navigational controls, and with a little effort they can also be quite useful for RSS feed generation. For more details about SiteMap files, see Automate Navigation Chores.

Generating an RSS Feed from a SiteMap File

The RSS.aspx.vb file shown in Figure 7 consumes a standard two-level SiteMap file (as shown in Figure 6) and generates an XML RSS feed from it.  Nearly identical code is used to create the RSS feed for this site.

Imports System.Xml

 

Partial Class RSS

  Inherits System.Web.UI.Page

 

  Protected Sub Page_Load(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles Me.Load

 

      'Clear the response and prepare it for Xml output

      Response.Clear()

      Response.ContentType = "text/xml"

      Response.AddHeader("Content-Disposition", _

        "inline;filename=rss.xml")

 

      'Instantiate an Xml Text Writer object

      Dim xtw As XmlTextWriter = _

        New XmlTextWriter(Response.OutputStream, _

        Encoding.UTF8)

      xtw.WriteStartDocument()

 

      'Required RSS tags

      xtw.WriteStartElement("rss")

      xtw.WriteAttributeString("version", "2.0")

 

      'Create Channel Tags

      xtw.WriteStartElement("channel")

      xtw.WriteElementString("title", "Some Web Site")

      xtw.WriteElementString("link", "http://SomeWeb.net")

      xtw.WriteElementString("description", "Techie Stuff")

      xtw.WriteElementString("language", "en-US")

 

      'Output each item

      For Each smnTop As SiteMapNode _

       In SiteMap.RootNode.ChildNodes

        For Each smn As SiteMapNode In smnTop.ChildNodes

          xtw.WriteStartElement("item")

          xtw.WriteElementString("title", smn.Title)

          xtw.WriteElementString("description", _

            smn.Description)

          xtw.WriteElementString("category", smnTop.Title)

 

          'Generate the item’s URL

          Dim baseUri As New Uri("http://SomeWeb.net")

          Dim myUri As New Uri(baseUri, smn.Url)

          xtw.WriteElementString("link", myUri.AbsoluteUri)

 

          'Close the item tag

          xtw.WriteEndElement()

        Next

      Next

 

      'Close each remaining tag

      xtw.WriteEndElement()

      xtw.WriteEndElement()

      xtw.WriteEndDocument()

      xtw.Flush()

      xtw.Close()

      Response.End()

  End Sub

End Class

Figure 7: This ASPX page processes the web application’s SiteMap file and generates an RSS feed from it.

The page starts by clearing the Response object because we don’t want any of the usual HTML headers to be output since the goal is to output an XML file. The next two lines in the first code block also aim to make it clear to the browser that this is to be interpreted as an XML document, not a Web page.

The second code block of Figure 7 instantiates an Xml Text Writer object so that it can later be filled with the resulting RSS formatted XML. The third code block outputs the initial XML element, which identifies it as an RSS 2.0 formatted XML file.

The fourth code block of Figure 7 creates the Channel element and all of its sub elements (except the Item list), These sub elements specify the title of the channel, the URL that the channel represents, a description of the channel, and the language in which it is published.

The main loop in Figure 7 outputs each item in the two-level hierarchy of the SiteMap shown in Figure 6. Each Item has a title, description, category, and link sub element. A few lines of code are needed to calculate the item’s absolute URL, because relative paths are not very useful in an RSS feed.

Finally, each XML element tag is closed. The resulting RSS document is shown in Figure 8, which can be consumed by any standard RSS-reading software, including Internet Explorer 7.

<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0">

<channel>

  <title>Some Web Site</title>

  <link>http://SomeWeb.net</link>

  <description>Computer Stuff</description>

  <language>en-US</language>

<item>

  <title>Financial</title>

  <description>Financial</description>

  <category>Software</category>

  <link>http://someweb.net/RSS/default4.aspx</link>

</item>

<item>

  <title>Office Suites</title>

  <description>Office Suites</description>

  <category>Software</category>

  <link>http://someweb.net/RSS/default5.aspx</link>

</item>

<item>

  <title>Computers</title>

  <description>Computers</description>

  <category>Hardware</category>

  <link>http://someweb.net/RSS/default8.aspx</link>

</item>

<item>

  <title>Keyboards</title>

  <description>Keyboards</description>

  <category>Hardware</category>

  <link>http://someweb.net/RSS/default9.aspx</link>

</item>

<item>

  <title>Fiction</title>

  <description>Fiction</description>