Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Creating an RSS Feed in ASP.NET
 

Creating an RSS Feed in ASP.NET

by Faisal Khan.Follow Faisal Khan on Twitter

RSS stands for "Really Simple Syndication". It is an XML file format which allows content creators to publish/syndicate content to their users. The users can subscribe to this content using their browsers or other desktop applications. This subscription allows the users to view and organize the updated content in their browsers or other desktop applications without actually visiting the website. They can then decide to visit the website and view the article(s) that has been updated in the feed. Users can subscribe to RSS feeds from multiple websites and see only the content that has been updated since their last visit to those websites or since the time they last viewed their contents in the feeds. After reading this article, you will be able to create an RSS feed for your website using a single ASP.NET page.

In this article, we will create an RSS XML file dynamically from an ASP.NET page displaying a list of latest 10 articles published at Stardeveloper.com. We will cache this list in Cache object to improve performance. This RSS file will be updated every 60 minutes. The users will be able to subscribe to this feed using their browsers. All modern browsers display an RSS icon when the user is visiting a website that makes available a feed. Upon clicking that RSS icon, the feed is displayed in the browser. How does a browser know that a website is making available a feed? We'll come to that later in the article.

Stardeveloper.com RSS Web Feed
Before we proceed any further, let us look at what you see in your browser when you access home page of Stardeveloper.com. In the Safari web browser, you can clearly see a blue icon displaying RSS as shown below:

RSS Feed Syndication Icon when Accessing Stardeveloper.com

When you click on this blue RSS icon, a page opens up showing you information about latest articles at Stardeveloper.com:

An ASP.NET Page Generating RSS Feed at Stardeveloper.com

Creating an RSS ASP.NET Web Feed
Create a new text file and save it as "rss.aspx" in your ASP.NET web application. For Stardeveloper.com, I placed it in "/articles" sub-folder of the main application. Copy and paste following code in it:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
  void Page_Load(object source, EventArgs e)
  {
    if (Cache["Rss"] == null)
    {

      Response.ContentType = "text/xml";
      Response.ContentEncoding = Encoding.UTF8;

      string channelTitle = "Stardeveloper.com Headlines";
      string channelLink = "http://www.stardeveloper.com";
      string channelDesc = "Get latest articles and information about ASP, " +
        "ASP.NET and JSP from Stardeveloper.com.";
      string language = "en-us";
      int ttl = 60; // Time to live, in minutes.
      string copyright = "Copyright 1999 - " + DateTime.Now.Year +
        " Stardeveloper.com, All Rights Reserved.";
        
      string connstr = ConfigurationManager.ConnectionStrings["YourConnStr"]
        .ConnectionString;
      string sql = "SELECT TOP 10 ArticleID, Title, Body, DatePublished, " +
        "Author FROM Articles ORDER BY DatePublished DESC";

      StringBuilder buffer = new StringBuilder();

      buffer.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
      buffer.Append("<rss version=\"2.0\">\r\n");
      buffer.Append("<channel>\r\n");

      buffer.Append("<title>").Append(channelTitle).Append("</title>\r\n");
      buffer.Append("<link>").Append(channelLink).Append("</link>\r\n");
      buffer.Append("<description>").Append(channelDesc);
      buffer.Append("</description>\r\n");
      buffer.Append("<language>").Append(language).Append("</language>\r\n");
      buffer.Append("<ttl>").Append(ttl).Append("</ttl>\r\n");
      buffer.Append("<copyright>").Append(copyright);
      buffer.Append("</copyright>\r\n");

      using (SqlConnection con = new SqlConnection(connstr))
      using (SqlCommand cmd = new SqlCommand(sql, con))
      {
        con.Open();

        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleResult);

        if (dr.HasRows)
        {
          bool firstRow = true;

          while (dr.Read())
          {
            int articleId = dr.GetInt32(0);
            string articleTitle = dr.GetString(1);
            string articleSynopsis = dr.GetString(2);
            DateTime datePublished = dr.GetDateTime(3);
            string author = dr.GetString(4);

            if (firstRow == true)
            {
              buffer.Append("<lastBuildDate>");
              buffer.Append(datePublished.ToString("r"));
              buffer.Append("</lastBuildDate>\r\n");
            }

            buffer.Append("<item>\r\n");
            buffer.Append("<title>").Append(articleTitle);
            buffer.Append("</title>\r\n");
            buffer.Append("<link><![CDATA[");
            buffer.Append(String.Format("http://www.stardeveloper.com/" +
              "articles/display.html?article={0}&page=1", articleId));
            buffer.Append("]]></link>\r\n");
            buffer.Append("<description><![CDATA[");
            buffer.Append(articleSynopsis);
            buffer.Append("]]></description>\r\n");
            buffer.Append("<author>").Append(author).Append("</author>\r\n");
            buffer.Append("<pubDate>");
            buffer.Append(datePublished.ToString("r"));
            buffer.Append("</pubDate>\r\n");
            buffer.Append("</item>\r\n");

            firstRow = false;
          }
        }
      }

      buffer.Append("</channel>\r\n");
      buffer.Append("</rss>\r\n");

      Cache.Insert("Rss", buffer.ToString(), null,
        DateTime.Now.AddHours(1), TimeSpan.Zero);
    }

    Response.Write(Cache["Rss"].ToString());
  }
</script>

RSS stands for "Really Simple Syndication". It is an XML file format which allows content creators to publish/syndicate content to their users. The users can subscribe to this content using their browsers or other desktop applications. This subscription allows the users to view and organize the updated content in their browsers or other desktop applications without actually visiting the website. They can then decide to visit the website and view the article(s) that has been updated in the feed. Users can subscribe to RSS feeds from multiple websites and see only the content that has been updated since their last visit to those websites or since the time they last viewed their contents in the feeds. After reading this article, you will be able to create an RSS feed for your website using a single ASP.NET page.

In this article, we will create an RSS XML file dynamically from an ASP.NET page displaying a list of latest 10 articles published at Stardeveloper.com. We will cache this list in Cache object to improve performance. This RSS file will be updated every 60 minutes. The users will be able to subscribe to this feed using their browsers. All modern browsers display an RSS icon when the user is visiting a website that makes available a feed. Upon clicking that RSS icon, the feed is displayed in the browser. How does a browser know that a website is making available a feed? We'll come to that later in the article.

Stardeveloper.com RSS Web Feed
Before we proceed any further, let us look at what you see in your browser when you access home page of Stardeveloper.com. In the Safari web browser, you can clearly see a blue icon displaying RSS as shown below:

RSS Feed Syndication Icon when Accessing Stardeveloper.com

When you click on this blue RSS icon, a page opens up showing you information about latest articles at Stardeveloper.com:

An ASP.NET Page Generating RSS Feed at Stardeveloper.com

Creating an RSS ASP.NET Web Feed
Create a new text file and save it as "rss.aspx" in your ASP.NET web application. For Stardeveloper.com, I placed it in "/articles" sub-folder of the main application. Copy and paste following code in it:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Xml" %>

<script runat="server">
  void Page_Load(object source, EventArgs e)
  {
    if (Cache["Rss"] == null)
    {

      Response.ContentType = "text/xml";
      Response.ContentEncoding = Encoding.UTF8;

      string channelTitle = "Stardeveloper.com Headlines";
      string channelLink = "http://www.stardeveloper.com";
      string channelDesc = "Get latest articles and information about ASP, " +
        "ASP.NET and JSP from Stardeveloper.com.";
      string language = "en-us";
      int ttl = 60; // Time to live, in minutes.
      string copyright = "Copyright 1999 - " + DateTime.Now.Year +
        " Stardeveloper.com, All Rights Reserved.";
        
      string connstr = ConfigurationManager.ConnectionStrings["YourConnStr"]
        .ConnectionString;
      string sql = "SELECT TOP 10 ArticleID, Title, Body, DatePublished, " +
        "Author FROM Articles ORDER BY DatePublished DESC";

      StringBuilder buffer = new StringBuilder();

      buffer.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
      buffer.Append("<rss version=\"2.0\">\r\n");
      buffer.Append("<channel>\r\n");

      buffer.Append("<title>").Append(channelTitle).Append("</title>\r\n");
      buffer.Append("<link>").Append(channelLink).Append("</link>\r\n");
      buffer.Append("<description>").Append(channelDesc);
      buffer.Append("</description>\r\n");
      buffer.Append("<language>").Append(language).Append("</language>\r\n");
      buffer.Append("<ttl>").Append(ttl).Append("</ttl>\r\n");
      buffer.Append("<copyright>").Append(copyright);
      buffer.Append("</copyright>\r\n");

      using (SqlConnection con = new SqlConnection(connstr))
      using (SqlCommand cmd = new SqlCommand(sql, con))
      {
        con.Open();

        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleResult);

        if (dr.HasRows)
        {
          bool firstRow = true;

          while (dr.Read())
          {
            int articleId = dr.GetInt32(0);
            string articleTitle = dr.GetString(1);
            string articleSynopsis = dr.GetString(2);
            DateTime datePublished = dr.GetDateTime(3);
            string author = dr.GetString(4);

            if (firstRow == true)
            {
              buffer.Append("<lastBuildDate>");
              buffer.Append(datePublished.ToString("r"));
              buffer.Append("</lastBuildDate>\r\n");
            }

            buffer.Append("<item>\r\n");
            buffer.Append("<title>").Append(articleTitle);
            buffer.Append("</title>\r\n");
            buffer.Append("<link><![CDATA[");
            buffer.Append(String.Format("http://www.stardeveloper.com/" +
              "articles/display.html?article={0}&page=1", articleId));
            buffer.Append("]]></link>\r\n");
            buffer.Append("<description><![CDATA[");
            buffer.Append(articleSynopsis);
            buffer.Append("]]></description>\r\n");
            buffer.Append("<author>").Append(author).Append("</author>\r\n");
            buffer.Append("<pubDate>");
            buffer.Append(datePublished.ToString("r"));
            buffer.Append("</pubDate>\r\n");
            buffer.Append("</item>\r\n");

            firstRow = false;
          }
        }
      }

      buffer.Append("</channel>\r\n");
      buffer.Append("</rss>\r\n");

      Cache.Insert("Rss", buffer.ToString(), null,
        DateTime.Now.AddHours(1), TimeSpan.Zero);
    }

    Response.Write(Cache["Rss"].ToString());
  }
</script>

 ( 1 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions

No Comments Found.


Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.