Click here to Skip to main content
15,885,216 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.4K   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;

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

    }
    protected void btnPublish_Click(object sender, EventArgs e)
    {
        Response.Clear();
        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 I am binding grid using SQLdatasource, This is for the example purpose reading from gridview 
        //it is not recomended use gridview, datatables,dataset to write data
        // 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();
        RSSFeed.Close();
        Response.Write("RSS Feed is created. For reading Rss feed <a href='RSSRead.aspx'>Click here.</a>");
    }
}

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