Click here to Skip to main content
15,883,705 members
Articles / Web Development / ASP.NET

What is RSS, and reading and writing RSS feeds

Rate me:
Please Sign up or sign in to vote.
4.40/5 (9 votes)
24 Feb 2010CPOL6 min read 100.3K   1.3K   47  
This article explains what RSS is and how we can read and write RSS feeds.
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.Text;
using System.Net;
using System.IO;

public partial class RSSRead : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnRead_Click(object sender, EventArgs e)
    {
        //  create here table and columns that will be used 
        //  for storing items from the xml file for later binding into grid 
        DataTable dtable = new DataTable();
        dtable.Columns.Add(new DataColumn("ProductName"));
        dtable.Columns.Add(new DataColumn("UnitPrice"));


        //  fetch webrequest. Here, give the path of the location where rss feed is stored.
        WebRequest WebReq = WebRequest.Create("http://localhost:1086/RSS/AvgPriceList.xml");
        
        //  get webresponse from the webrequset 
        WebResponse webRes = WebReq.GetResponse();

        //  use stream to stremline the input from from webresponse.
        Stream rssStream = webRes.GetResponseStream();

        //  Create new xml document and load a XML Document
        //  with the strem.
        XmlDocument xmlDoc = new XmlDocument();

        //  loads the url from the stream
        xmlDoc.Load(rssStream);

        //  use XmlNodeList to get the matching xmlnodes from the xmldocument
        XmlNodeList xmlNodeList = xmlDoc.SelectNodes("rss/channel/item");

        //  create array of the object for creating the row
        object[] RowValues = { "", "" };

        //  Make a Loop through RSS Feed items
        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();
        }
        //  finally bind the grid....
        grvRSS.DataSource = dtable;
        grvRSS.DataBind();
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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