Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to create RSS feed in ASP.NET

Author: Richard Wand
Download Source Code : 1393_CreateRSSFeed.zip

This article will explain how we can create RSS feed in ASP.NET.

 

Really Simple Syndication (RSS) provides a standard to make information available for user who requests for your RSS feed. It can be used to publish data that is frequently updated such as news and blog entries. Data for RSS feed must be in XML format and RSS feed creates XML files of your data by following certain structure. This structure includes necessary tag which is rss and other tags like channel, item, title, description and link. Each channel element can have one or more item elements and each item element has three required tags. Those required tags are title, description and link. You can also include tags like author and category in it.
 
In this article, I will create a sample RSS feed for articles written on DevASP.NET. I will use a table from SQL Server database to get data for RSS feed. The data for RSS feed will be changed every time when the data in SQL Server database table have changed. This requires frequent requests of data from database so it in case of heavy traffic, we need to handle the situation. We can add OutputCache directive at page level to cache data for specific time.

 

Creating RSS feed in ASP.NET is easy.

  1. Create new website and add the following OutputCache directive in aspx page of a web form

    <%@ OutputCache Duration="120" VaryByParam="none" %>

     
  2. I have created a database ASP.NETArticles and a table ArticlesList. Table fields are listed below with data type.

    Id
    int
    AuthorName
    varchar(30)
    Category
    varchar(10)
    Title
    nvarchar(100)
    Description
    nvarchar(200)
    URL
    nvarchar(100)
     
  3. Write code below in code behind file of C Sharp or Visual Basic

    C#
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    using System.Xml;
    using System.Text;
     
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=local;Initial Catalog=ASP.NETArticles;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("SELECT * FROM ArticlesList", conn);
            conn.Open();
     
            Response.Clear();
            Response.ContentType = "text/xml; charset=utf-8";
            XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("rss");
            xmlWriter.WriteAttributeString("version", "2.0");
            xmlWriter.WriteStartElement("channel");
            xmlWriter.WriteElementString("title", "Read ASP.NET, C#, VB.NET, SQL Server, Web Services Articles on DevASP.NET");
            xmlWriter.WriteElementString("link", "DevASP.NET");
            xmlWriter.WriteElementString("description", "DevASP.NET contains articles on ASP.NET, C#, VB.NET, SQL Server and Web Services categories");
     
            SqlDataReader dr = cmd.ExecuteReader();
     
            while (dr.Read())
            {
                xmlWriter.WriteStartElement("item");
                xmlWriter.WriteElementString("title", dr["Title"].ToString());
                xmlWriter.WriteElementString("author", dr["AuthorName"].ToString());
                xmlWriter.WriteElementString("category", dr["Category"].ToString());
                xmlWriter.WriteElementString("description", dr["Description"].ToString());
                xmlWriter.WriteElementString("link", dr["URL"].ToString());
                xmlWriter.WriteEndElement();
            }
            conn.Close();
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndElement();
            xmlWriter.Flush();
            xmlWriter.Close();
            Response.End();
        }
     
    }
     
    VB.NET
     
    Imports System.Data.SqlClient
    Imports System.Xml
    Imports System.Text
     
    Partial Class _Default
        Inherits System.Web.UI.Page
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim conn As New SqlConnection("Data Source=local;Initial Catalog=ASP.NETArticles;Integrated Security=True")
            Dim cmd As New SqlCommand("SELECT * FROM ArticlesList", conn)
            conn.Open()
     
            Response.Clear()
            Response.ContentType = "text/xml; charset=utf-8"
            Dim xmlWriter As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
            xmlWriter.WriteStartDocument()
            xmlWriter.WriteStartElement("rss")
            xmlWriter.WriteAttributeString("version", "2.0")
            xmlWriter.WriteStartElement("channel")
            xmlWriter.WriteElementString("title", "Read ASP.NET, C#, VB.NET, SQL Server, Web Services Articles on DevASP.NET")
            xmlWriter.WriteElementString("link", "DevASP.NET")
            xmlWriter.WriteElementString("description", "DevASP.NET contains articles on ASP.NET, C#, VB.NET, SQL Server and Web Services categories")
     
            Dim dr As SqlDataReader = cmd.ExecuteReader()
     
            While dr.Read()
                xmlWriter.WriteStartElement("item")
                xmlWriter.WriteElementString("title", dr("Title").ToString())
                xmlWriter.WriteElementString("author", dr("AuthorName").ToString())
                xmlWriter.WriteElementString("category", dr("Category").ToString())
                xmlWriter.WriteElementString("description", dr("Description").ToString())
                xmlWriter.WriteElementString("link", dr("URL").ToString())
                xmlWriter.WriteEndElement()
            End While
            conn.Close()
            xmlWriter.WriteEndElement()
            xmlWriter.WriteEndElement()
            xmlWriter.Flush()
            xmlWriter.Close()
            Response.End()
        End Sub
    End Class
     
    First set connection to your server and get data from SQL Server database table. I have used ExecuteReader() method to get data from the table. Clear content output from buffer and set the content type of Response as “text/xml”. Create an XmlTextWriter object and call WriteStartDocument() method. Start rss and channel tags and also write attribute string for rss version. Call WriteElementString() method for title, link and description. Start item tag and call WriteElementString() method for title, author, category, description and link for every item with their values from database table. At the end, close database connection and write end elements. Flush and close XmlTextWriter object and call the End() method of Response object to send all currently buffered output to client.
     
  4. Now start debugging and see result of RSS feed

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to import data from Excel to Access database in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net