Click here to Skip to main content
Click here to Skip to main content
Go to top

What is RSS, and reading and writing RSS feeds

, 24 Feb 2010
Rate this:
Please Sign up or sign in to vote.
This article explains what RSS is and how we can read and write RSS feeds.

Introduction

In this article, I am trying to explain what an RSS feed is, and how we can read and write RSS feeds.

A number of websites, news services, and blogs provide RSS content. RSS is a method of syndicating content. RSS stands for Really Simple Syndication (or Rich Site Summary, and also RDF Site Summary). Nowadays, RSS has become the dominant format for distributing updated web contents on the Web. RSS is a protocol, an application of XML which provides an open method for making web feeds (it is a data format used for providing users with frequently updated content) available from a site in order to provide other people with a summary of the website's recently added content.

RSS feeds make a portion of a website available to other sites or individual subscribers. RSS can also be used to describe different type of licensing website content so that other websites can use it. Lots of news-related sites, weblogs, and other online publishers syndicate their content like headlines, links, and article summaries as an RSS feed to whoever wants it. Thus, users can have constantly updated content from websites without keep checking back to any particular site to see if it's been updated. RSS is changing the world of publishing news and searching for news. Thus, RSS feeds are the most beneficial and faster way of submitting updated content to subscribers.

For people who regularly use websites, RSS allow them to easily stay informed by retrieving the latest content from their interested sites. Users save time by not needing to visit each site individually. We can ensure privacy by not needing to join each site's email newsletter. Moreover, RSS subscribers can choose the content they wish to view - this adds into the increase in popularity of RSS feeds.

There are several different versions of RSS, falling into two major branches: RDF and 2.*. The following image will show different versions of RSS.

RSSVersions.JPG

Why we should use RSS feeds

We should use RSS to get updated information for the following reasons:

  • Feeds show all updated information in a single file at a specific location.
  • RSS feeds make it possible to view brief information about the content without visiting a webpage.

How RSS feeds work

  • News-related sites, weblogs, and other online publishers create their updated web content with headlines, links, descriptions, summaries etc. in XML format, and they provide a link to that file.
  • A Feed Aggregator collects the feed and creates a topic-wise index on the available feeds.
  • A Feed Reader reads the feeds and displays those feeds to the user in a prescribed format.
  • User can view the updated content from their PC with the help of the Feed Reader.
  • The links in a feed are used for further reading if the user is interested.

RSS Aggregator

RSS aggregator, popularly known as feed reader, news reader, RSS reader, or simply aggregator, is a client software or a web application which aggregates syndicated web content such as headlines, links, and article summaries, and blogs in a single location for easy viewing.

Aggregators save time and effort required for regular checking of websites for updates, thus creating a separate space. When we subscribe to a feed, an aggregator checks for new content at specific time intervals usually specified by the user, and retrieves updates. With the aggregator, we can easily unsubscribe the feeds as compared to email-subscription or news-letters. This method is also referred to as pulling contents from a website.

The following image explains how an RSS feed works:

RSSWorking.JPG

How to create an RSS feed

RSS feeds use XML format. XML is short for Extensible Markup Language, in which we can create our own tags. RSS feeds use some special tags for creating updated content. RSS feeds follow a very simple structure, and can be understood very easily.

An RSS feed uses tags in brackets <> (like in HTML) for defining its contents. Lots of RSS tags are optional. Following is a summary of the different RSS tags:

The contents of RSS feeds are called items. These items are combined with related data. Every item contains three or more sub-items. The common of those are:

  • Title - Same as the name of the website.
  • Description - A brief information about the content.
  • Link - The referral URL where the actual content is located.

We use title and description to write a brief summary of the contents. And, we link references to the webpage that contains the actual data. Similar to HTML, RSS feed XML uses open and close tags for title, description, and link. E.g.: <channel/>, <title/>, <link/>, <description/>. An RSS feed is a continuous sequence of the items.

RSS feed channel

A channel is container of items; in other words, all items in an RSS feed make a channel. A channel shows how items co-relate with each other. Channels also have a title, description, and tags, similar to items. Along with these elements, a channel can have some more elements:

  • pubDate - The date of content publication.
  • language - Language of the channel.
  • Copyright - Copyright notice.
  • Webmaster - The email address that can be used for dealing with technical problems.
  • Category - Denotes the category of the contents.
  • Generator - Contains the name of the program that created the feed.
  • Skip Days - Shows the number of days when you do not need to check for changes in the feed.
  • Cloud - Enables us to register with a cloud server, for getting informed about changes in the feeds. This saves time as we don't required to check for updates.

You can find more about these tags here.......

In the demo website, I have given two examples:

  • Simple - Feed reading and writing.
  • Cricinfo website RSS - All Cricinfo website live scorecards by RSS.

Code for writing an RSS feed

I have used the Northwind database for this example, and the Products Above Average Price view to create the RSS feed. I have bound the GridView with a SQL Server data source.

XmlTextWriter RSSFeed = new XmlTextWriter(
  System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + 
  "AvgPriceList.xml",Encoding.UTF8);
// Write the rss tags like title, version,
// Channel, title, link description copyright etc. 
RSSFeed.WriteStartDocument();
RSSFeed.WriteStartElement("rss");
RSSFeed.WriteAttributeString("version", "2.0");
RSSFeed.WriteStartElement("channel");
RSSFeed.WriteElementString("title", "Products Above Average Prices");
RSSFeed.WriteElementString("link", "http://localhost/RSSFeeds");
RSSFeed.WriteElementString("description", 
        "Details of all the Products Average Prices");
RSSFeed.WriteElementString("copyright", "Copyright Indrajeeth 2010");

Here is how we get values from the GridView to write into the RSS feed:

//here I am binding grid using SQLdatasource,
//For the example purpose reading from gridview         
//Loop through the Gridview of the database and add them to the RSS feed

foreach (GridViewRow grvRow in grvRSS.Rows)
{
    RSSFeed.WriteStartElement("item");
    RSSFeed.WriteElementString("ProductName", grvRow.Cells[0].Text);
    RSSFeed.WriteElementString("UnitPrice", grvRow.Cells[1].Text);
    RSSFeed.WriteElementString("Link", 
       "http://localhost/RSSFeeds/ProductList.xml");
    RSSFeed.WriteElementString("PublishDate", DateTime.Now.ToString());
    RSSFeed.WriteEndElement();
}
RSSFeed.WriteEndElement();
RSSFeed.WriteEndElement();
RSSFeed.WriteEndDocument();
RSSFeed.Flush();

Reading an RSS feed

  1. We first create the table and columns that will be used for storing the items from the XML file for later binding into the grid:
  2. DataTable dtable = new DataTable();
    dtable.Columns.Add(new DataColumn("ProductName"));
    dtable.Columns.Add(new DataColumn("UnitPrice"));
  3. Create a WebRequest. Here, give the location where the RSS feed is stored, and get a WebResponse from the WebRequset.
  4. WebRequest WebReq = 
      WebRequest.Create("http://localhost:1086/RSS/AvgPriceList.xml");
    WebResponse webRes = WebReq.GetResponse();
  5. Use Stream to get the input from the WebResponse. Create a new XML document and load the XML document with the Stream.
  6. Stream rssStream = webRes.GetResponseStream();
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(rssStream);
  7. Use an XmlNodeList to get the matching XML nodes from the XmlDocument.
  8. XmlNodeList xmlNodeList = xmlDoc.SelectNodes("rss/channel/item");
  9. Create an array of objects for creating the row, and loop through the RSS feed items:
  10. for (int i = 0; i < xmlNodeList.Count; i++)
    {
        XmlNode xmlNode;
    
        xmlNode = xmlNodeList.Item(i).SelectSingleNode("ProductName");
        if (xmlNode != null)
        {
            RowValues[0] = xmlNode.InnerText;
        }
        else
        {
            RowValues[0] = "";
        }
    
        xmlNode = xmlNodeList.Item(i).SelectSingleNode("UnitPrice");
        if (xmlNode != null)
        {
            RowValues[1] = xmlNode.InnerText;
        }
        else
        {
            RowValues[1] = "";
        }
        //  creating datarow and add it to the datatable
        DataRow dRow;
        dRow = dtable.Rows.Add(RowValues);
        dtable.AcceptChanges();
    }
  11. Finally, bind the grid:
  12. grvRSS.DataSource = dtable;
    grvRSS.DataBind();

Results

Writing the RSS feed

RSSWriting.JPG

Reading the RSS feed

RSSReading.JPG

Reading the Cricinfo website RSS feed

RSSCricinfo.JPG

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Indrajeet Sutar
Software Developer (Junior) Excel Informatics, Pune
India India
Am Indrajeet T. Sutar. I am a web developer. I am working on .net technology. I have completed my Masters in Computers and Management (people call it as MCA, MBA etc.) Apart from programming i do photography (not regularly though), traveling and reading books.

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermkomasi31-Oct-10 20:51 
GeneralWCF Syndication was built for this PinmemberPCoffey26-Feb-10 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 24 Feb 2010
Article Copyright 2010 by Indrajeet Sutar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid