SS (Really Simple Syndication) feeds are an easy way for organizations to distribute content. At the most basic level, an RSS feed, or channel, is an XML document containing a list of titles, descriptions, and links to content. Any client that can parse XML can read and format the content to display the titles, descriptions, and links in a custom context, such as within a <div>
tag on a Web page or on a Windows form.
To use an RSS feed, you need to:
Create the context.
Decide what you want to do with the feed and how you want it to look.
Retrieve the XML feed.
All you need is the URL for the feed location. Then you make a standard HTTP GET request for the XML data. You can treat the result as a file, writing it to local storage, or as transient in-memory data.
Parse and format the results.
To display the contents, you need to parse the XML document, extracting and formatting the information you want to display. The simplest way to do that is to use an XSLT stylesheet to control the display characteristics.
|Editor's Note: If you're interested in using the DevX content feeds either individually or on a Web site, visit our RSS FAQ page.
RSS 2.0 Document Structure
|Figure 1. Viewing the XML: In the sample application, you can hit the "Show RSS Document" button to invoke the getXMLFeed method, which displays the raw XML.|
RSS has gone through three versions
since its inception. DevX's feeds
adhere to the latest specification, RSS 2.0
Let's begin by running through the basic structure of a feed. An RSS 2.0 document must begin with the standard XML declaration containing version and encoding attributes.
<?xml version="1.0" encoding="ISO-8859-1" ?>
The next tag is an <rss> element containing a single <channel> child element. The <channel> element must contain title, link, and description elements. These typically contain the title of the feed source organization, a link to that organization, and a description of the feed contents. For example, DevX's <channel> element looks like this:
<title>DevX: Latest Published Articles</title>
<description>Latest DevX Content</description>
... <!-- more channel information here -->
In addition to the three required elements, the <channel> element may contain a number of optional elements;. DevX uses these additional elements:
<copyright>Copyright 2002 DevX</copyright>
<lastBuildDate>Wed, 18 Jun 2003
The optional elements <language>, <copyright>, and <image> are mostly self-explanatory. The <copyright> element tells you the source of the feed while the <image> element is used in our case to display the DevX logo.
The <docs> element is documentation for the RSS version and usually links to the Radio UserLand RSS page as shown. The <lastBuildDate> element is particularly important for automation purposes. Some organizations, such as those with stock market feeds, update their RSS feeds often; others, such as DevX, update the feeds on a daily basis, or only when information changes. If you're caching feeds information locally, you only need to update the cache when the value of the <lastBuildDate> element changes.
The <channel> element also contains a list of <item> elements. These are the individual pieces of content. All <item> child elements are optional, but each <item> must contain, at minimum, a <title> or a <description> element. In practice, most <item> elements contain a title, a link, and a description. Again, the RSS 2.0 specification lists optional <item> elements. DevX uses the following <item> structure.
<pubDate>Thu, 12 Jun 2003 13:23:27 PST</pubDate>
Obtaining a DevX RSS Feed with .NET
Obtaining the content of a DevX RSS feed (or performing any HTTP GET request) with .NET is straightforward. You simply add a reference to the System.Net namespace to your class or module.
After doing that, you can use the WebClient object in the System.Net namespace to make requests to a URL and retrieve the results. The following getXMLFeed method uses a WebClient to request the XML data. The method might just as easily be called "DoHTMLGetRequest," because it isn't specific to XML nor to RSS feedsit just makes a GET request and returns the result.
Public Function getXMLFeed( _
ByVal URL As String) As String
Dim wc As New WebClient
Dim b() As Byte
Dim sXML As String = String.Empty
b = wc.DownloadData(URL)
sXML = System.Text.Encoding.ASCII.GetString(b)
The sample application in this article (and in the downloadable source code, left) obtains the RSS documents from DevX using the getXMLFeed method.