Developing a Simple RSS Reader in .NET

No.of Views4802
Bookmarked0 times
Downloads 
Votes0
By  Geming Leader   On  10 Sep 2010 07:09:25
Tag : XML , How to
This writing does not include a full discussion or even the full details of RSS or XML. Rather, it includes a nice introduction to RSS and its XML schema. In addition, it incorporates what you get in a sample application that is easy-to-code, understand, and to extend.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

This article is also available in my blog, Just Like a Magic.

Contents

Contents of this writing:

  • Contents
  • Overview
  • Definition
  • Feeds
  • Aggregators/Readers
  • Version History
  • Schema
  • Sample; RSS Bars Library
  • Download
  • Further Readings

Overview

This writing does not include a full discussion or even the full details of RSS or XML. Rather, it includes a nice introduction to RSS and its XML schema. In addition, it incorporates what you get in a sample application that is easy-to-code, understand, and to extend.

Definition

RSS (commonly expanded as Really Simple Syndication or, sometimes, Rich Site Summary) is a XML content with specific schema used to deliver frequently changing web content (like news headlines, blogs, etc.).

RSS content is also known as a feed, web feed, syndication feed, web syndication, and a channel. It is widely known and distinguished by its icon '

RSS feeds are usually files that reside in a specific location. Those files usually (not extensively) has the extension rss or xml.

Feeds

Today, most -if not all - of the blogs and sites that have frequently changing web content incorporate RSS.

For instance, The New York Times has more than one hundred of RSS feeds available for subscription (listed here.) Every feed delivers the latest headlines for a specific category (Technology, Sports, etc.)

Aggregators/Readers

How can you benefits from RSS feeds? Surely, XML data is not the flexible and readable content that can be used. Thus, users usually access feeds via applications (or web clients) that are known as Feed Readers, RSS Readers, and Aggregators. Those applications read the RSS XML content, parse it, and display feed items (e.g. news headlines) to the user in a friendly interface.

Version History

RSS undergoes several changes that result in different versions and two major branches:

  1. RDF (Resource Description Framework) or RSS 1.* in other words.
  2. RSS 2.*

We will assume RSS 2.* is our discussion.

Schema

As any other XML format, RSS has a specific schema that RSS contents (i.e. feeds) should comply with; they required to implement obligatory elements, and they had the choice to implement other optional elements.

As a matter of discussion, we will take the CodeGain latest articles RSS feed (available here) as an example and extract the RSS schema from it.

The following is a sample from the CodeGain RSS feed content (at the time of this writing):

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
  <channel>
    <title>The Code Gain Latest Articles</title>
    <link>http://www.codegain.com</link>
    <description>Latest Articles from The CodeGain</description>
    <language>en-us</language>
    <copyright>Copyright &amp;copy; CodeGain, 2009-2010</copyright>
    <webMaster>info@codegain.com</webMaster>
    <lastBuildDate>Fri, 10 Sep 2010 07:00:07 GMT</lastBuildDate>
    <ttl>30</ttl>
    <image>
      <title>The CodeGain</title>
      <url>http://www.codegain.com/images/logo.png</url>
      <link>http://www.codegain.com</link>
      <width>261</width>
      <height>90</height>
      <description>The CodeGain</description>
    </image>
    <item>
      <title>Developing a Simple RSS Reader in .NET</title>
      <description>This writing does not include a full discussion or even the full details of RSS or XML. Rather, it includes a nice introduction to RSS and its XML schema. In addition, it incorporates what you get in a sample application that is easy-to-code, understand, and to extend.</description>
      <link>http://www.codegain.com/articles/xml/howto/introducing-rss-developing-a-simple-rss-reader.aspx</link>
      <author>Geming Leader                 </author>
      <pubDate>10 Sep 2010 07:55:25</pubDate>
      <subject>XML</subject>
      <GUID>http://www.codegain.com/articles/xml/howto/introducing-rss-developing-a-simple-rss-reader.aspx</GUID>
    </item>
    <item>
      <title>Assembly Spy; a Reflection Sample</title>
      <description>Assembly Spy is a very nice simple application written in VB.NET that uses reflection to dynamically inspect assemblies and list the containing types and members of the selected type.</description>
      <link>http://www.codegain.com/articles/dotnetframeworks/miscellaneous/assembly-spy-a-reflection-sample.aspx</link>
      <author>Geming Leader                 </author>
      <pubDate>10 Sep 2010 07:51:20</pubDate>
      <subject>.NET Frameworks</subject>
      <GUID>http://www.codegain.com/articles/dotnetframeworks/miscellaneous/assembly-spy-a-reflection-sample.aspx</GUID>
    </item>
    <item>
      <title>How to Create WCF 4.0 Service and Hosting in IIS 7.5</title>
      <description>In this article, i have walkthrough  on creating WCF service in .NET 4.0 and host in IIS 7.5</description>
      <link>http://www.codegain.com/articles/wcf/miscellaneous/how-to-create-wcf-4-0-service-and-hosting-in-iis-7-5.aspx</link>
      <author>dhananjaycoder                </author>
      <pubDate>09 Sep 2010 10:21:25</pubDate>
      <subject>WCF</subject>
      <GUID>http://www.codegain.com/articles/wcf/miscellaneous/how-to-create-wcf-4-0-service-and-hosting-in-iis-7-5.aspx</GUID>
    </item>
</chnnel>
</rss>

 

If we have a look at the file in XML Notepad we can see the following results:

To gain more understanding of the schema, let's have a look at this diagram:

Required elements surrounded by the red border. Many other optional elements are available.

Most elements are self-explanatory from their names. However, the following maybe need more explanation:

  • rss\channel\language: Content language (e.g. en-us for English for United States.)
  • rss\channel\lastBuildDate: The date of the last change of the content.
  • rss\channel\ttl: Time to Live. The number of minutes that indicate how long a channel can be cached before refreshing from the source. You would ignore this element, in many cases.
  • rss\channel\generator: The name of the program used to generate this feed.
  • rss\channel\item\guid: A Globally Unique Identifier used to identify this feed item.

Sample; RSS Bars Library

Our sample project is not an application in itself. Actually it is a WinForms control library that is called Geming.WinForms.RssBars. This library includes bars that read RSS content from a specific feeds and display it to the user.

This is an extensible library you can extend it to read from any RSS feed you like. The following are snapshots of the RssBars controls (reading CodeGain, Just Like a Magic, BBC, and the Nile News channels.)

Image Loading
Image Loading
Image Loading

The following figure shows library class diagram:

As you see, we have only one business object, RssItem structure. It encapsulates fields related to a feed item.

The RssBar is the base MustInherit (abstract in C#) class. It defines the base functionality of a RSS bar. All other classes are just children of the base class. They incorporate the functionality of RssBar by just setting its RSS feed path.

The RssBar requires two parameters for instantiation, the RSS path, and the banner image. For the sake of performance, we have required the developer to pass a banner image of the feed instead of automatically loading it from the image element of the feed content.

To avoid duplication, members of the RssBar are documented in the code.

The core function of the RssBar is the ReadRss() function. This function accesses the RSS feed and populates the list of feed items and display them to the user.

Since RSS is a XML format, we will need to reference the System.Xml.dll library as it is the core library for accessing XML via .NET. (Do not forget to import the System.Xml namespace.)

The following is a sample from the function code. Code abbreviated for clarity.

Public Sub ReadRss()
    ' Checking design mode, exit if True

    ' Preparing the screen

    ' The XML Document
    Dim xmlDoc As New Xml.XmlDocument
    ' Loading the RSS feed, should fail if network is not available
    xmlDoc.Load(m_rss)

    ' Accessing the €œchannel€ element
    Dim ndChannel As Xml.XmlNode = xmlDoc.Item("rss").Item("channel")

    ' Comparing publication date to one we have
    ' continue if something new

    ' Item cocollection
    Dim collItems As New Collections.Generic.List(Of RssItem)
    Dim ndItem As Xml.XmlNode

    ' Enumerating through the items
    ' and populating the collection
    For i As Integer = 0 To ndChannel.ChildNodes.Count - 1
        ndItem = ndChannel.ChildNodes(i)
        If (ndItem.Name = "item") Then
            collItems.Add( _
            NewRssItem(ndItem.Item("title").ChildNodes(0).InnerText, _
            ndItem.Item("link").ChildNodes(0).InnerText, _
            ndItem.Item("description").ChildNodes(0).InnerText))
        End If
    Next

    ' Checking if items cound

    ' Clear existing items

    Do While enumerator.MoveNext

        ' Item

        lbl = New Label
        ' Creating a Label for the item
        ' and filling its fields
        Me.CtrlsPanel.Controls.Add(lbl)

        ' Adding handlers, so we could fire events
        AddHandler lbl.Click, AddressOf Me.RssItem_Click
        AddHandler lbl.MouseMove, AddressOf Me.RssItem_MouseMove

        ' Image
        image = New PictureBox
        ' Adding the banner image between items
        Me.CtrlsPanel.Controls.Add(image)

        ' Adding event handlers, so we could fire events
        AddHandler image.Click, AddressOf Image_Click
        AddHandler image.MouseMove, AddressOf Image_MouseMove
    Loop

    ' Finalization
End Sub

Child classes simply do not contain any code, just the line that instantiates the base RssBar and sets the RSS feed path and image. For instance,

Public Sub New()
MyBase.New("http://www.codegain.com/Common/rss.aspx", m_image)
Me.RightToLeft = Windows.Forms.RightToLeft.No
End Sub

Download

Download source files -86 kb

Further Readings

Need more about RSS? Here are a few good references:

 
Sign Up to vote for this article
 
About Author
 
Geming Leader
Occupation-Software Engineer
Company-Just Like a Magic
Member Type-Expert
Location-Egypt
Joined date-30 Jul 2009
Home Page-http://WithDotNet.net
Blog Page-http://JustLikeAMagic.com
Independent software developer, trainer, and technical writer from Egypt born in 1991
 
 
Other popularSectionarticles
    Many times we have a scenario like we need to use some hardcode values like our SQL connection string or some List name or any configuration settings like network credentials.
    Published Date : 11/Jun/2010
    Validate XML over DTD
    Published Date : 15/Feb/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</