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
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
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.
The next tag is an
DevX: Latest Published Articleshttp://www.devx.com Latest DevX Content...
In addition to the three required elements, the
en-us Copyright 2002 DevX http://backend.userland.com/rss Wed, 18 Jun 2003 23:30:51 PST DevX http://services.devx.com/outgoing/devxwire.gifhttp://www.DevX.com
The optional elements
Title here DescriptionURL Author(s) names Thu, 12 Jun 2003 13:23:27 PST
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 feeds?it 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) Return sXML End Function
The sample application in this article (and in the downloadable source code, left) obtains the RSS documents from DevX using the getXMLFeed method.
Transforming the RSS Feed to HTML
Now that you’ve obtained the RSS document, you need to write the instructions to display the contents. There are a number of sites on the Web that will perform RSS-to-HTML transformations for you, and DevX exposes the feeds as HTML directly, in a form suitable for inclusion in an . However, you can easily construct your own XSLT stylesheet to customize the display. For example, the following template creates a 300-pixel wide
Unrecognized RSS feed version.
The stylesheet shown above has four templates that matches the main RSS elements?the ubiquitous root template (match=”/”), which matches the root node of the document, the
- When the parser encounters an
element, it checks the version attribute value. If it’s not 2.0, the stylesheet returns an error; otherwise, it parses the element
- When the stylesheet encounters the
element, it creates the 300-pixel widetag and displays the value of the channel element, formatted as a link (using the element as the link destination), and the copyright notice. Next, it performs the command, which selects each
- element, in turn.
- When the stylesheet encounters an
- element, it displays the title as a link and the description.
Perform the XSLT Transform
There are three sample projects in the downloadable code for this article. The first is a class library project containing a class called DevXRSS that exposes the getXMLFeed method shown earlier in this article. It also exposes a getHTMLFeed method which retrieves the raw XML RSS document, and then applies the XSLT stylesheet shown in the preceding section of this article. As you’ll see, you don’t need a special wrapper function to retrieve and display DevX’s pre-built HTML. The other two projects are functionally equivalent Windows Forms and Web Forms (ASP.NET) applications that display the feeds in various forms.
Public Function getHTMLFeed(ByVal URL As String) _ As String Dim erReader As New EmbeddedResourceReader Dim s As String = _ erReader. _ GetResourceStringFromExecutingAssembly _ ("DevXRSS.DevXRSS.xsl") If s.Length > 0 Then ' load the XSLT Stylesheet Dim xsl As New XslTransform Dim st As New StringReader(s) xsl.Load(New XmlTextReader(st), _ Nothing, [Assembly]. _ GetExecutingAssembly.Evidence) ' retrieve the xml Dim xml As New XmlDocument xml.LoadXml(Me.getXMLFeed(URL)) ' perform the XSL transform Dim args As New XsltArgumentList Dim wrtr As XmlTextWriter Dim sb As New StringBuilder Dim sw As New StringWriter(sb) wrtr = New XmlTextWriter(sw) xsl.Transform(xml.CreateNavigator(), _ args, wrtr, New XmlUrlResolver) Dim sRet As String = sb.ToString sw.Close() wrtr.Close() Return sRet End If End Function
The stylesheet itself (DevxRSS.xsl) is an embedded file. The method retrieves the string contents using the EmbeddedResourceReader class included with the project, and then loads that string into a new XslTransform instance. It retrieves the RSS document using the getXMLFeed method, and then calls one of the XslTransform object’s overloaded Transform methods, passing in an XmlNavigator, an empty XsltArgumentList (required, but not used here), an XmlTextWriter instance, and a new XmlUrlResolver instance (required, but not used here). This particular version of the Transform method uses an XmlTextWriter to write the transformed content to an underlying StringBuilder. The method returns the StringBuilder contents.
DevX RSS Feed Examples
You can use the feeds in either a Windows Forms application or an ASP.NET application. The sample Windows Forms project, called TestDevXRSS, contains one form with an embedded WebBrowser control, and three buttons. The first button, “Show RSS Document,” calls the getXMLFeed method and displays the raw XML in the embedded browser (see Figure 1).
The second button, “Show Transformed RSS” calls the getHTMLFeed method and displays the result of the XSLT Transform (see Figure 2).
The third button “Show DevX HTML Feed” simply tells the browser to navigate to the URL that returns the DevX-generated HTML for the feed (see Figure 3).
An ASP.NET Web project (also included with the sample code) relies on the same feeds and the same transformation as the Windows Forms project. Figure 4 shows the DevX HTML feed in the IFRAME.
The ASP.NET application’s functionality is essentially identical to that of the Windows Forms application. While the Windows Forms application uses an embedded WebBrowser control to display the content, the Web Forms application uses an IFRAME (which is essentially a WebBrowser control embedded in the browser’s window) to display the same content.
When a user clicks one of the buttons in the Web Form project, the form posts back to the server. The code-behind for each button uses a Label control on the page as a placeholder to insert an
Using both Windows Forms and Web Forms should serve to illustrate how easy it is to display RSS content and how XML is transforming the concepts of programming. XML, as RSS feeds or as Web services, is changing the way people design and develop applications
We encourage you to include DevX’s feeds in your own applications. Besides the general feed, which includes all feature articles and tutorials, as they are published, the following technology-specific feeds are also available:
- Web Development