Creating an RSS feed reader in C#

This tutorial will show you how to create your own RSS reader using the XmlTextReader in .NET. You'll learn how to read information about the RSS feed, retrieve its main content (the items) and output it in a ListView.

The stucture of an RSS feed

RSS feeds are everywhere, almost any website with dynamic content has at least one RSS feed for the visitors to subscribe to, so there’s no question why would you need to know how to build an RSS feed reader. I don’t think this is news to you, but an RSS feed is actually an XML file that follows a certain structure.

RSS feeds can have their own custom tags, or lack some of the tags, but the main structure should be the same: start with an xml tag, followed by an xml-stylesheet tag (if applicable) and then the rss tag which tells us it’s an RSS feed. The comes the content inside the channel tag. The nodes under the channel tag give information about the feed, such as its title and description. Then comes the item tags which have children tags which represent the main content of the feed: the title, link and description tags.

Below is a screenshot of the application that I built for this tutorial and which you can download from the link at the top of the project. It’s compiled using .NET 2.0, but you can compile the code with a .NET 1.1 compiler too.

I suggest you download the code first (even if you have Visual Studio .NET 1.1) because I’m not going to enumerate the controls on the form and their names.

Coding the RSS reader

Here comes the fun part. Coding :). Do not be frightened by the if’s and for’s that we’re about to witness.

First, the System.Xml namespace. We couldn’t do without it…

using System.Xml;

A bunch of variable we’ll use:

XmlTextReader rssReader;

XmlDocument rssDoc;

XmlNode nodeRss;

XmlNode nodeChannel;

XmlNode nodeItem;

ListViewItem rowNews;
The nodes (nodeRss, nodeChannel, nodeItem) will be set to the actual nodes inside the XML file (<rss>,<channel> and <item>).

Most of the action takes place in the click event of the Read button (btnRead):

// First clear of any previous items in the ListView

lstNews.Items.Clear();

this.Cursor = Cursors.WaitCursor;

// Create a new XmlTextReader from the specified URL (RSS feed)

rssReader = new XmlTextReader(txtUrl.Text);

rssDoc = new XmlDocument();

// Load the XML content into a XmlDocument

rssDoc.Load(rssReader);



// Loop for the <rss> tag

for (int i = 0; i < rssDoc.ChildNodes.Count; i++)

{

   // If it is the rss tag

   if (rssDoc.ChildNodes[i].Name == “rss”)

   {

      // <rss> tag found

      nodeRss = rssDoc.ChildNodes[i];

   }

}
// Loop for the <channel> tag

for (int i = 0; i < nodeRss.ChildNodes.Count; i++)

{

   // If it is the channel tag

   if (nodeRss.ChildNodes[i].Name == “channel”)

   {

      // <channel> tag found

      nodeChannel = nodeRss.ChildNodes[i];

   }

}
// Set the labels with information from inside the nodes

lblTitle.Text = “Title: “ + nodeChannel[“title”].InnerText;

lblLanguage.Text = “Language: “ + nodeChannel[“language”].InnerText;

lblLink.Text = “Link: “ + nodeChannel[“link”].InnerText;

lblDescription.Text = “Description: “ + nodeChannel[“description”].InnerText;

// Loop for the <title>, <link>, <description> and all the other tags

for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)

{

   // If it is the item tag, then it has children tags which we will add as items to the ListView

   if (nodeChannel.ChildNodes[i].Name == “item”)

   {

      nodeItem = nodeChannel.ChildNodes[i];



      // Create a new row in the ListView containing information from inside the nodes

      rowNews = new ListViewItem();

      rowNews.Text = nodeItem[“title”].InnerText;

      rowNews.SubItems.Add(nodeItem[“link”].InnerText);

      lstNews.Items.Add(rowNews);

   }

}
this.Cursor = Cursors.Default;

You can see here
how we load the content of the supplied RSS feed (via txtUrl)
into an XmlDocument.
Then we simply star looking for tags by looping through the nodes in search of the node. We never know how many nodes there
are before the node, that’s why we have to take each and every node and see if it’s the one we want ( in our case). Once we found it, we store it’s content (child nodes) within nodeRss. Now it would be the time to get information such as the RSS version the feed is using from the attributes of the tag, however we’ll skip that since we want to keep this tutorial simple.

Now we loop again, this time in search of the <channel> tag.

We proceed with the same technique we used for the <rss> tag: store it as a XmlNode.

Next thing you know, we are actually retrieving data from the RSS feed and outputing it using the labels on the form. More exactly, we retrieve information about the RSS feed from the first tags inside <channel>
(marked with blue in the picture at the top of the tutorial). We retrieve the Title, Language, Link and Description of the RSS feed, but we could retrieve much more such as the last update
date or the feed generator, depending on the feed.

The next big step follows, loop through the child nodes (<child>), get the information inside them (<title> and <link>) and add it to the ListView.

This is enough for the RSS reader to work. Running the application, entering a valid feed isnide txtUrl and pressing the Read Feed button will read the feed. It will take a few seconds to retrieve the feed’s content from the Internet, and putting this on a different thread would be a good idea.

Now we have one more thing to do. When a row is selected inside the ListView, we’ll want to give more details about that item in the feed, such as the description. So let’s do that inside the SelectedIndexChanged event of the ListView:

// When an items is selected

if (lstNews.SelectedItems.Count == 1)

{

   // Loop through all the nodes under <channel>

   for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)

   {

      // Until you find the <item> node

      if (nodeChannel.ChildNodes[i].Name == “item”)

      {

         // Store the item as a node

         nodeItem = nodeChannel.ChildNodes[i];

         // If the <title> tag matches the current selected item

         if (nodeItem[“title”].InnerText == lstNews.SelectedItems[0].Text)

         {

            // It’s the item we were looking for, get the description

            txtContent.Text = nodeItem[“description”].InnerText;

            // We don’t need to loop anymore

            break;

         }

      }

   }

}

What we’re doing here is loop through all the <item> tags and check to see if their <title> tag inside matches the value of the currently selected item in the ListView. So basically we search the XML file for the title of the selected item to find its <description> tag and show it inside the RichTextBox. I know, this piece of code could use some improvement in terms of performance and reliability: what if there are multiple items in the feed with the same title? The application will show the description of the first one found. A solution to this would be to store the description from the first time we loop through the file (inside the Click event of btnRead), perhaps in a dataset or array. I will look into these further for a better solution.

There’s one more feature of the application, double clicking an item inside the ListView will open the link in your default browser. To do that simply add this into the DoubleClick event of the ListView:

// When double clicked open the web page

System.Diagnostics.Process.Start(lstNews.SelectedItems[0].SubItems[1].Text);
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top