devxlogo

Learn to Consume RSS Using DevX’s New Content Feeds

Learn to Consume RSS Using DevX’s New Content Feeds

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

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.

The next tag is an element containing a single child element. The 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 element looks like this:

    DevX: Latest Published Articles    http://www.devx.com    Latest DevX Content    ... 

In addition to the three required elements, the element may contain a number of optional elements;. DevX uses these additional elements:

    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.gif              http://www.DevX.com    

The optional elements , , and are mostly self-explanatory. The element tells you the source of the feed while the element is used in our case to display the DevX logo.

The element is documentation for the RSS version and usually links to the Radio UserLand RSS page as shown. The 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 element changes.

The element also contains a list of elements. These are the individual pieces of content. All child elements are optional, but each must contain, at minimum, a or a element. In practice, most elements contain a title, a link, and a description. Again, the RSS 2.0 specification lists optional elements. DevX uses the following structure.

  Title here          Description  URL  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.

Imports System.Net

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

containing the feed data, with the item titles formatted as links.

            

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 element, the element, and the element.

  • 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 wide
    tag 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

    Figure 2. Viewing the Feed: In the sample application, you can hit the “Show Transformed RSS” button to invoke the getHTMLFeed method, which displays the feed in human-readable HTML.

    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).

    Figure 3. Pre-formatted HTML: The Show DevX HTML Feed button uses an IFRAME tag to request the pre-formatted content from DevX. Although this is quick and convenient, you have little control over how the content appears.

    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

    Figure 4. ASP.NET Version: The ASP.NET version of the sample application is functionally identical to the Windows Forms version shown in Figures 1 through 3.

    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:

    • .NET
    • Java
    • XML
    • Database
    • Enterprise
    • C++
    • Web Development
    • Wireless

devx-admin

Share the Post: