asp:cover story
LANGUAGES: C#
ASP.NET VERSIONS: 1.0 | 1.1 | 2.0
Displaying Syndicated Content
RssFeed: An Open-source Control for Adding RSS Feeds to Your Site
By Scott Mitchell
As the quantity of information on the Internet has exponentially expanded year after year, staying current with the latest news and information has become more time consuming. To help aggregate constantly evolving information, there exist numerous Web sites that do nothing but collect the data, categorize it, and present it. Sites like Google News (http://news.google.com), and portals like Yahoo! and Lycos, provide a composite of the latest news, weather, sports scores, and breaking headlines.
Although these aggregation Web sites are a great one-stop place for a variety of information, it requires that you take the time to load your browser, visit the site, and peruse the information. Rather than having to periodically check the Web site to get the information, many users would like the information delivered to them as it happens. Many Web sites provide such functionality. CNN.com, for example, has a "breaking news" e-mail service, sending e-mails out to subscribers when breaking news unfolds. Many news Web sites also have a daily or weekly newsletter that highlights the day's or week's top news stories.
These e-mail-based techniques are adequate for pushing content to human readers, but what if you want also to provide this content in a form that computer programs can easily parse? There are many potential solutions; however, an easy, platform-neutral one to implement is to provide the content in an XML-formatted file. XML is the ideal choice for a variety of reasons. First, XML data is represented as text, not binary data. Since different computer architectures can represent binary data in different formats, using a text format removes the need for knowing what format the binary data is stored in, and - potentially - performing conversions. Second, there exist XML parsers for virtually every programming language and platform. Third, XML offers a high degree of flexibility in how data can be marked up, providing more freedom when deciding how to syndicate the content.
RSS: The Standard for Web Content Syndication
Over the past five years developers have worked on creating a standard XML structure for specifying syndicated content. The first such syndication format was formulated in 1999 by Netscape and called RDF Site Summary, abbreviated RSS. (RDF is a standard for providing information in a machine-readable, XML format. See http://www.w3.org/RDF for more information.) The RSS 0.9 specification then forked into two diverging paths: RSS 1.0 updated the RSS 0.9 spec adhering to the RDF spec as well; RSS 0.91, which today is at version 2.0, dropped support for RDF and changed the acronym's meaning from RDF Site Summary to Really Simple Syndication. The RSS 1.0 specification can be found at http://web.resource.org/rss/1.0/spec; the RSS 2.0 specification is available at http://blogs.law.harvard.edu/tech/rss.
More and more Web sites today use either RSS 1.0 or RSS 2.0 to syndicate their content. For example, Slashdot.org uses RSS 1.0 to syndicate their latest stories. CNet's News.com Web and Microsoft's MSDN Web site both use RSS 2.0 to syndicate their latest articles. Virtually all personal blogs provide syndication through RSS 2.0, as well. These Web sites provide this syndication data at a specified URL. Slashdot.org's RSS 1.0 feed, for example, is available at http://slashdot.org/index.rss. The RSS feed for MSDN's latest articles is at http://msdn.microsoft.com/rss.xml.
Data syndicated with RSS is typically consumed by RSS aggregators. An RSS aggregator is a program that periodically checks RSS feeds from a number of Web sites and, if any new content is found, saves or displays the new content for viewing. There are a number of standalone desktop RSS aggregators, such as FeedDemon (http://www.bradsoft.com/feeddemon), SharpReader (http://www.sharpreader.net), and RssBandit (http://www.rssbandit.org). To use these RSS aggregators you simply indicate which RSS feeds interest you. The RSS aggregator then periodically checks the list of RSS feeds and downloads any new content. You can then, at your leisure, browse through the headlines or articles for each site. There are also RSS aggregators that operate inside of Outlook, like NewsGator (http://www.newsgator.com), as well as online RSS aggregators, such as BlogLines (http://www.bloglines.com).
RSS syndicated data can also be displayed on a Web page. For example, if you run a Web site with information on building Web applications using ASP.NET, you might want to provide links to Microsoft's latest articles that appeared in the ASP.NET Developer Center on MSDN. Rather than having to periodically check Microsoft's site and manually add links, a simpler approach would be to simply display the articles syndicated in the ASP.NET Developer Center's RSS feed.
In this article we're going to look at the RSS 2.0 specification in detail. We'll examine how to read an RSS feed and display the contents in an ASP.NET Web page. We will also take a look at a free, open-source ASP.NET control I created, RssFeed. Using RssFeed you'll be able to quickly add professional-looking syndicated content to your Web site with only having to write two lines of code!
Learn the RSS 2.0 Specification
The RSS 2.0 specification was designed as a syndication
format for Web sites that publish new content on a regular basis. News sites,
blogs, weather sites, or sites that publish articles or other content often,
can benefit by syndicating their content online with RSS. Content is syndicated
using an XML document (see Figure 1). The root element is
http://www.4GuysFromRolla.com/
4GuysFromRolla.com
Corporation All Rights Reserved.
http://4guysfromrolla.com/012804-1.aspx
This article examines skmMenu, an open-source menu
control.
http://4guysfromrolla.com/011204-1.aspx
This article looks at how ASP.NET Web pages are
Handled by the IIS Web server.
http://4guysfromrolla.com/010704-1.aspx
Have you ever wondered how you can work with XML
data in an ASP.NET Web page? This article, by
Jisun Lee, examines the XML classes in the .NET
Framework. Read on to learn more!
Figure 1: An RSS 2.0 syndication feed contains an
Some of the
Element |
Description |
|
Name of the channel. For example, the |
|
URL of the site syndicating the content. |
|
Description of the site. |
|
Copyright notice. |
|
"Time to live;" value represents the number of minutes an RSS aggregator should cache the syndicated content before checking to see if there is new content. |
Figure 2: Selected RSS
The
Element |
Description |
|
Title of the content item. This might be the news headline or the name of the article. |
|
URL of the content item. |
|
Description of the content item. |
|
E-mail address of individual who authored the content item. |
|
Date and time the content item was published. The date and time must conform to the format outlined by RFC #822. |
Figure 3: Selected RSS
For each content item you want to syndicate, add an
Consume RSS in an ASP.NET Web Page
Although RSS syndication feeds are typically used to display content in a desktop application, RSS feeds can also be tapped by ASP.NET Web pages, thereby showing the latest headlines in a Web page. Typically sites that syndicate their content only provide the headline and a brief description of the news item or article. We can consume this RSS feed and display the headlines on our site as hyperlinks pointing back to the content item on the site from where the syndication comes. Syndicated-related content in your Web site offers two advantages:
- Your users benefit because your Web site now has information available.
- You, as the developer maintaining the Web site, benefit because the syndicated content is automatically pulled down. That is, you don't have to go find the latest articles and manually add them to your site.
The first challenge in displaying syndicated content in an ASP.NET Web page is somehow accessing the RSS feed's XML data from the Web site from where the content is being syndicated. Fortunately, the .NET Framework's classes that work with XML can easily slurp XML content from a remote URL.
The next challenge is deciding how to display the XML content. There are two basic approaches that can be used: one is to use an XSLT stylesheet to translate the RSS XML into HTML. Another approach is to iterate through the XML data and build up the proper markup for rendering the syndicated content.
For this article, let's examine how to render the content by building up the HTML content ourselves, iteratively. We're going to head down this path because our goal is to build a reusable, custom server control that displays content from an RSS feed. This custom, compiled control we'll be creating will iteratively build up its content based on the specified RSS feed, just like a DataGrid, DataList, or Repeater iteratively builds itself from the specified DataSource.
Load the XML and Iterate through
the
There are myriad classes in the .NET Framework for working with XML documents, each offering tradeoffs of functionality versus performance. The XmlTextReader class, for example, provides the most efficient access to reading an XML document, but is read-only and cannot navigate the document using XPath expressions. (XPath is a syntax for referencing particular elements in an XML document.) The XmlDocument class provides read/write access and XPath expressions, but is the least efficient of the XML-related classes. If you need read-only access with XPath expression capabilities, the XPathDocument class is the best choice, since it offers better performance than the XmlDocument class. (For a more thorough discussion on techniques for accessing XML data, be sure to read Aaron Skonnard's article ".NET XML Best Practices" at http://support.softartisans.com/kbview.aspx?ID=673.)
Let's create an ASP.NET Web page that allows the visitor to enter the URL to an RSS feed, and then displays the results of the feed in an HTML