Creating RSS Reader

In this article, I want to discuss about RSS. I want to explain what is RSS and why is it required. Then I want to show how you can write a program in ASP.NET to read RSS feed of a website and provide some information about that website in your website.

What is RSS?

RSS stands for Really Simple Syndication. It is based on XML. It is a format using which a website can provide news headlines to other website in XML format. For example, java.sun.com and msdn.microsoft.com etc. provide RSS feed. I can use a program to read these RSS feeds and displays headlines of those websites in my website. In fact, that's what I will do in this article.

RSS has become a common features of many websites. All most all major websites provide RSS feed so that other websites can get headlines of the website and provide access to those headlines. Website providing RSS include msdn.microsoft.com, java.sun.com, cnn.com, bbc.co.uk etc.

Format Of RSS

RSS has undergone a couple of changes in the last few years. I would like to stick to RSS version 2.0 in this article. RSS is a simple XML document. So every RSS complies with XML specifications.

Click here to see an example of RSS for srikanthtechnologies.com.

The following are important tags in RSS.

The root element of the document is rss.  It contains a mandatory attribute called  version, which contains the version that the document complies with.

Element channel is the first child element of rss. It has the following required child nodes. It also has a lot of optional child nodes such as language, webmaster etc. For a complete list of RSS 2.0 specification, click here.

title Title of the channel. Generally the title of the website.
link The url that refers to the website that provides channel.
description Description about the channel.

A channel contains a number of item elements. Each item element again contains a set of child nodes providing details about the item. An item may refer to a blog, article, news or just about anything that you want to share with others.

The following is the list of important child elements of item element.

title The title of the item.
link The URL of the item.
description Some description regarding the item.
author Email address of the author of the item.
pubDate Indicates when the item was published. Date and item should be in the standard format.

Creating RSS reader

Now, let us create an Asp.Net page that takes url regarding the RSS and then displays title and link related to all items of the RSS.

Here is the source code of rssreader.aspxClick to run RSSReader.aspx
    
    <%@ Page Language="VB"  Debug="true"%>
    <%@ import Namespace ="System.Xml" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
     Protected Sub btnRead_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim xmldoc As New XmlDocument()
        Dim items As XmlNodeList
        Dim item, node As XmlNode
        
        xmldoc.Load(txtRss.text)
        items = xmldoc.SelectNodes("/rss/channel/item")  ' use XPath to get only items
        
        Dim title = "", link = "", st As String = ""
        
        For Each item In items
            For Each node In item.ChildNodes
                If node.Name = "title" Then
                    Title = node.InnerText
                End If
                If node.Name = "link" Then
                    link = node.InnerText
                End If
            Next
            st &= "<a target='_blank' href='" & link & "'>" & title & "</a><br/>"
        Next
        lblOutput.Text = st
     End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
     <title>RSS Reader</title>
     <style>
      body {font:11pt verdana;}
     </style>
   </head>
   <body>
    <form id="form1" runat="server">
    <div>
        <h2>
        RSS Reader</h2>
        <br />
        Enter RSS URL :
        <asp:TextBox ID="txtRss" runat="server" Width="247px"></asp:TextBox>
        <asp:Button ID="btnRead" runat="server" Text="Read RSS" OnClick="btnRead_Click" /><br />
        <br />
        <br />
        <asp:Label ID="lblOutput" runat="server"></asp:Label></div>
    </form>
   </body>
  </html>

 
The above program takes a RSS feed URL from the user using a Textbox. When user clicks on button, it open the XML document provided by RSS feed and accesses the data using XMLDocument object.

The program uses XPATH to look for all item elements under rss/channel element.

It then displays all hyperlinks (anchors) using the  URL provided in link element and title provided by title element.

You can enter  msdn.microsoft.com/rss.xml for Microsoft MSDN and http://developers.sun.com/rss/sdn.xml for java.sun.com as input for RSS Feed and click on Read RSS button to check how the program works.

Keep Learning,
Srikanth.