To read the article online, visit http://www.4GuysFromRolla.com/webtech/031303-1.shtml

Syndicating Your Web Site's Content with RSS

By Scott Mitchell


For Syndicating RSS Content with ASP.NET...
This article examines syndicating Web site content with RSS using classic ASP. There is another article here on 4Guys - Syndicating Your Web Site's Content with RSS and ASP.NET - that looks at syndicating RSS using ASP.NET.

Introduction
RSS, or Really Simple Syndication, is a format designed to allow Web site content to be easily syndicated. The syndicated content can be integrated into other Web sites, or can be viewed by individuals via an assortment of desktop applications. For example, CNet's News.com site can be syndicated, meaning you can add the News.com's latest headlines to your Web site. A plethora of other Web sites, especially blogs, provide this syndication feature. (For example, you can syndicate the content from the myriad of Internet.com sites. Check out http://www.webreference.com/services/news/ for more information!)

In order to syndicate your Web site's content, all you need to do is create an RSS feed. An RSS feed is a Web-accessible file those wishing to consume your content may access. The file must be an XML file in the proper RSS standards format. Typically, an RSS feed is a static .xml that is periodically updated by some background process, but you can use ASP or ASP.NET Web pages to dynamically generate the RSS feed's content.

This article examines the RSS format and examines the RSS feed I created for ASPMessageboard.com. This article also discusses how to consume RSS feeds using ASP, and mentions various programs you can obtain to browse various RSS feeds from your desktop.

For More on RSS...
For more information on RSS be sure to read: What is RSS?. A good description of the RSS spec can be found at http://blogs.law.harvard.edu/tech/rss

Creating an RSS Feed
The first step in syndicating your Web site's content is creating an RSS feed. Keep in mind that an RSS feed is nothing more than an XML file that contains certain required tags. An example RSS feed can be seen below:

<?xml version="1.0" ?>
<rss version="2.0">
  <channel>
    <title>This is my first RSS feed!</title>
    <link>http://www.myWebSite.com</link>
    <description>I am testing out creating an RSS feed...</description>
 
    <item>
      <title>The First Item to Check Out</title>
      <link>http://www.myWebSite.com/story1.htm</link>
      <description>This is the first article on my Web site!</description>
    </item>

    <item>
      <title>The Second Item to Check Out</title>
      <link>http://www.myWebSite.com/story1.htm</link>
      <description>This is the second article on my Web site!</description>
    </item>

    ...
  </channel>
</rss>

Note that the RSS file has as its root the <rss> node. Following this there is one <channel> node. Inside the channel node there must be the <title>, <link>, and <description> nodes. The actual content that you want to syndicate appears in <item> tags. Each <item> tag must have at least an <title> or <description> tag. (For a much more thorough discussion of the RSS spec, be sure to check out http://backend.userland.com/rss.)

A site like CNN.com might have numerous RSS feeds: one for World news, one for US news, one for Politics, one for Sports, and so on. Each RSS feed would then have, perhaps, 10 <item> tags, one for each of the 10 latest stories. This RSS feed could be generated in one of two ways:

  • The feed could be generated as a static .xml file every half hour by some background process, or
  • The feed could be dynamically generated every time it is requested by having the RSS feed be an ASP page that generates the needed XML

What option is the better one? Well, it depends on how often your content is updated. If you are only adding new content once a day, or a couple times a day, then the static approach is likely to be ideal. However, if the content you are adding changes frequently, a dynamic RSS feed might be the better option.

Creating an RSS Feed for ASPMessageboard.com
I recently decided to add an XML feed to the ASPMessageboard. Currently, the ASPMessageboard has a Recent Questions page that lists the 25 most recently posted questions/answers on the messageboard. I thought this would be a great item for syndication.

I started by creating a file named rss.asp, and then cut and pasted the code in from the existing RecentQuestions.asp Web page. All I had to do was cut out the HTML and replace it with the appropriate XML. The following code shows the germane parts of rss.asp:

<?xml version="1.0" encoding="ISO-8859-1"?>
<% Response.Buffer = true
   Response.ContentType = "text/xml"
   
Function ApplyXMLFormatting(strInput)
  strInput = Replace(strInput,"&", "&amp;")
  strInput = Replace(strInput,"'", "'")
  strInput = Replace(strInput,"""", "&quot;")
  strInput = Replace(strInput, ">", "&gt;")
  strInput = Replace(strInput,"<","&lt;")
  
  ApplyXMLFormatting = strInput
End Function   
    %>

<rss version="2.0">
  <channel>
    <title>Recent Posts on ASPMessageboard.com</title>
    <link>http://www.aspmessageboard.com/</link>
    <description>ASPMessageboard.com is a site where developers of 
       Microsoft's Active Server Pages and Active Server Pages .NET 
       can come to ask questions and get answers!</description>
    <language>en-us</language>
    <copyright>Copyright 2003 Jupitermedia Corporation. 
       All Rights Reserved.</copyright>
    <lastBuildDate><%=Now()%></lastBuildDate>
    <ttl>20</ttl>
<%
  Dim objConn
  Set objConn = Server.CreateObject("ADODB.Connection")
  objConn.ConnectionString = Application("forum_system_dsn")
  objConn.Open
    
  Dim objRS, strSQL, strDesc 
  strSQL = "sp_GetTop25Posts"
  Set objRS = objConn.Execute(strSQL)  

  Do While Not objRS.EOF
    strDesc = "<b>Message by " & objRS("MsgAuthor") & " on " & _
             objRS("MsgDate") & " EST</b><br>" & _
             objRS("MsgResponse").Value %>
    <item>
      <title><%=ApplyXMLFormatting(objRS("MsgTitle").Value)%> 
         (<%=ApplyXMLFormatting(objRS("MsgAuthor").Value)%>)
      </title>
      <link>http://www.aspmessageboard.com/forum/showMessage.asp?F=
           <%=objRS("ForumID")%>&amp;P=1&amp;M=<%=objRS("MsgID")%></link>
      <description><%=ApplyXMLFormatting(strDesc)%></description>
      <datePosted><%=ApplyXMLFormatting(objRS("MsgDate"))%></datePosted>
    </item>
    <%
    objRS.MoveNext
  Loop
  
  objRS.Close
  Set objRS = Nothing

  objConn.Close
  Set objRS = Nothing
  Set objConn = Nothing
%>
  </channel>
</rss>
[View the RSS Feed!]

One very important thing to realize is that XML files cannot contain <, >, or & characters. Rather, they must be replaced by &lt;, &gt;, and &amp;, respectively. (Technically, XML files are also supposed to be free of apostrophes and double quotes, to be replaced with &apos; and &quot, respectively. However, many XML parsers aren't as strict about this condition.) Since these forbidden characters may occur in the post's title, author, or body, before emitting any of these values, they are passed through the ApplyXMLFormatting() function, which replaces any occurrences of the offensive characters.

Also note that the <?xml version="1.0" encoding="ISO-8859-1"?> tag appears at the top of the ASP page. This is because certain browsers, such as Mozilla, will complain if there is any content before the <?xml ?> processing directive. Mozilla considers even whitespace to be content that cannot appear before the directive. This is why <?xml version="1.0" encoding="ISO-8859-1"?> goes at the very top.

Date Format for <lastBuildDate>
Alert 4Guys reader Kevin Latham notes that the date format is not correct for the <lastBuildDate> date. Rather, the date format must appear in the following format:

DayAbbreviation, DayOfMonth MonthAbbreviation Year TimeInGMT

(This format can be seen in the RSS specs.) I fixed this in the actual ASPMessageboard.com RSS feed by using a custom date formatting function written by Ken Schaefer in the article A Customizable Date Formatting Routine. (Note that I did not show this fix in the code example above because it would add much length to the code example... just be sure you have your dates formatted properly.)

Consuming the Syndicated RSS Feed
Once you have syndicated your Web site's content using RSS you may wonder how others can consume the syndicated material. Typically, RSS feeds are consumed by other Web sites. For example, using CNN's RSS feed, one could add the latest headlines to their Web site. If you are interested in consuming an RSS feed, check out the RSS Newsgrabber, which is an ASP example authored by Eric Walters. There's also examples in both ASP and ASP.NET by Stephen Travis at Gathering News Headline Feeds using ASP.

In addition to being able to consume RSS feeds via a Web interface, you can view RSS feeds via desktop applications as well. For example, NewsGator, a commercial product, provides RSS feed integration with Outlook. There are also free RSS feed readers, like FreeReader.

Conclusion
In this article we examined how to syndicate Web site content using an RSS feed. Creating an RSS feed for discretizable information is quite simple: all we need to do is place each unit of information in an <item> tag. This is an especially easy task if your data is stored in a database, as we saw by examining the RSS feed for ASPMessageboard.com.

Consuming an RSS Feed with ASP.NET
Consuming an RSS feed and displaying it in a DataGrid is insanely easy with ASP.NET! It's so amazingly simple I decided to write an article about it: Consuming an RSS Feed with ASP.NET. Read on for more details!

Also, if you are interested in learning how to create an RSS syndication feed with ASP.NET, check out my article: Creating an Online RSS News Aggregator with ASP.NET.

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


  • Article Information
    Article Title: Syndicating Your Web Site's Content with RSS
    Article Author: Scott Mitchell
    Published Date: Thursday, March 13, 2003
    Article URL: http://www.4GuysFromRolla.com/webtech/031303-1.shtml


    Copyright 2014 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers