As you probably know, RSS stands for Really Simple Syndication. I am sure you are subscribing to many feeds (which are RSS documents) that keep you posted on your favourite subjects and/or websites updates. It is a very simple mechanism of notification. These notifications are published using a set of standards formats so they can be processed by any application that requires it.
It was never said that those notifications where for humans only. Your very own applications can also benefits from this simple mechanism.
This article will show you how to consume RSS feeds from any source using the System.Net.WebRequest namespace.
Demo application
This month, the demo application is provided in both VB and C#. The solution was saved using Visual Studio 2008.
What does a RSS documents contain?
A RSS document (or a feed) follows a set of standards so that they are widely available. When you broadcast something on a RSS channel, you want to reach a maximum number of recipients.
A document always contains metadata of the message such as the author, the publication date and a text zone (either a full text or a summarized version with a link to the full content). This document is always in a structured XML format.
Code to support RSS feeds
You will need a set of short classes to consume RSS feeds. I have implemented these 2 classes into the same file (cRSS.vb or cRSS.cs).
The first one (cRssChannel) is the longest one as it contains some the data about the channel itself and a method to query the channel and feed the properties. Here is the code:
Public Class cRssChannel Public Title As String Public Link As System.Uri Public Description As String Public Items As List(Of cItem) Public Sub New() Me.Items = New List(Of cItem) End Sub Public Function QueryRSS(ByVal url As System.Uri) As Boolean 'Create a WebRequest object using the URI Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url.AbsoluteUri) 'Query the channel Dim response As System.Net.WebResponse = request.GetResponse() Dim xmlDoc As New System.Xml.XmlDocument() xmlDoc.Load(response.GetResponseStream()) Dim rssElement As System.Xml.XmlElement = xmlDoc("rss") If rssElement Is Nothing Then 'This is not a RSS channel Return False End If Dim channelElement As System.Xml.XmlElement = rssElement("channel") If channelElement Is Nothing Then 'This is not a RSS channel Return False Else ' Create the channel and set attributes Me.Title = channelElement("title").InnerText Me.Link = New Uri(channelElement("link").InnerText) Me.Description = channelElement("description").InnerText ' Read the content Dim itemElements As System.Xml.XmlNodeList = channelElement.GetElementsByTagName("item") For Each itemElement As System.Xml.XmlElement In itemElements Dim item As New cItem() With { _ .Title = itemElement("title").InnerText, _ .Link = itemElement("link").InnerText, _ .Description = itemElement("description").InnerText, _ .PubDate = itemElement("pubDate").InnerText _ } Me.Items.Add(item) Next Return True End If End Function End Class
As you can see in the QueryRSS method, a simple WebRequest object is created. The response variable contains whatever was returned from the URI. This response is loaded into a XML document and validated to check if it contains a RSS node and a CHANNEL node. If it is the case, every item are loaded into the Items collection.
The second and last class (cItem) contains one single feed’s data. The previous class (cRssChannel) has a collection of them. Here is the code:
Public Class cItem Public Title As String Public Link As String Public Description As String Public PubDate As String End Class
The User interface
The user interface is really simple. It shows a textbox to let the user type in the URL of RSS feed, a button to launch the action and another textbox to display the resulting items.
Figure 1: The demo application querying my own RSS feed
The only code in this form is behind the Button’s Click event. It reads like this:
Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click 'Clear the content of the resulting textbox txtResults.Clear() 'Query the RSS channel Dim objChannel As New cRssChannel If objChannel.QueryRSS(New Uri(txtURL.Text)) Then 'Display results txtResults.Text += "Title: " + objChannel.Title + Environment.NewLine txtResults.Text += "Link: " + objChannel.Link.ToString + Environment.NewLine txtResults.Text += "Description: " + objChannel.Description + Environment.NewLine txtResults.Text += Environment.NewLine txtResults.Text += "# items: " + objChannel.Items.Count.ToString + Environment.NewLine txtResults.Text += Environment.NewLine Dim intItem As Integer = 1 For Each x As cItem In objChannel.Items txtResults.Text += " Item # " + intItem.ToString + Environment.NewLine txtResults.Text += " Title: " + x.Title + Environment.NewLine txtResults.Text += " Link: " + x.Link + Environment.NewLine txtResults.Text += " Description: " + x.Description + Environment.NewLine txtResults.Text += " PubDate: " + x.PubDate + Environment.NewLine txtResults.Text += Environment.NewLine intItem += 1 Next Else txtResults.Text += "Invalid request." End If End Sub
As you can see, it is nothing more than going through the properties and the list of items read from RSS feed and displaying them in the textbox.
Other resources
If you want to push further the use of a RSS feed, you should take a look at the ASP.Net RSS toolkit published on CodePlex. This toolkit let you consume and publish RSS content.
Even if its name specifically mentions ASP.Net, it also provides a means of being used from Windows applications.
Conclusion
RSS feeds are everywhere and chances are that you may get something interesting for your application to consume is greater than ever. By chance, with some call to built-in System.Net.WebRequest, wecan easily wrap it.