CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Creating RSS Feeds using Repeater and SqlDataSource controls in ASP.Net

By Satheesh Babu
Posted On Mar 30,2009
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

Creating RSS Feeds using Repeater and SqlDataSource controls in ASP.Net

 

Introduction

RSS feed is an xml formatted document that gives the capability to read frequently updated contents without visiting our site. RSS feed has become one of the sophisticated mechanism to share information on the internet world. It is also one of the web 2.0 features which allow users to read the relevant information’s available on the internet. All we need to do is get the RSS feed URL from different sites of our interest and subscribe it on our RSS reader. In this article, we will see how to implement a RSS feed for our site and make it available for others to consume it.

Before moving to actual implementation of RSS feed, we will understand the format of RSS feeds that a RSS reader can understand.

 

For example, CodeDigest RSS feed will look like,

<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0">

  <channel>

  <title>CodeDigest.com Latest Articles</title>

  <link>http://www.codedigest.com</link>

  <description>Latest articles hosted on CodeDigest.com.</description>

  <item>

  <title>Useful Datagrid Tips</title>

  <description>There are some frequent queries that are being asked in
most of the forums and UG’s regarding datagrid. I have compiled a list of useful
tips from my previous posts and posted it here.</description>

  <link>http://www.codedigest.com/Articles/ASPNET/77_Useful_Datagrid_Tips.aspx</link>

  <pubDate>4/29/2008 9:47:12 AM</pubDate>

  </item>

</channel>

  </rss>

 

As we can see the above XML, the whole contents are packed inside <rss> tag followed by <channel> tag. And the actual information about the posts are kept inside <item> tag which in turn is packed inside a <channel> tag. Read the history of RSS here and specification of RSS here.

 

To construct RSS feed; we need to form the above xml for the frequently updated data and emit it. In this article, we will use Repeater and SqlDataSource control to construct the RSS XML and emit it.

 

Steps

1.      Open Visual Studio 2005.

2.      Select File>New>Website. Select ASP.Net Website and provide a name for your project.

3.      Select a language of your choice, I have selected C#. Click OK.

4.      Drag a Repeater control and SqlDataSource control from Data tab of the Visual Studio toolbox.

5.      Configure the SqlDataSource control to select the required data for constructing RSS feed. For easy understanding, I have a created a Sql Express database with a table "Articles" in App_Data folder of the sample project. 

6.      Configure the DataSourceID of the Repeater control to SqlDataSource control’s ID. Refer the below code,

                                                                                                                                                             




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RSS.aspx.cs" Inherits="_Default" %>

<asp:Repeater id="Repeater1" runat="server" DataSourceID="SqlDataSource1">

<HeaderTemplate>

<rss version="2.0">

  <channel>

    <title>CodeDigest.Com Article Feeds</title>

    <description></description>   

    <copyright></copyright>  

</HeaderTemplate>

<ItemTemplate>

  <item>

    <title><%# Eval("Title")%></title>

    <link><%# Eval("URL")%></link>   

    <pubDate><%# Eval("Submittedon", "{0:R}")%></pubDate>

    <category><%# Eval("Category")%></category>

    <description><%# Eval("Title")%></description>

  </item>

</ItemTemplate>

<FooterTemplate>

  </channel>

</rss>   

</FooterTemplate>

</asp:Repeater>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"

ProviderName="System.Data.SqlClient" SelectCommand="SELECT ArticleID,Title,Category,Author,URL,Submittedon FROM Articles">

</asp:SqlDataSource>

 

The ASPX page should have only @Page directive and the controls. We can remove all the other tags(head,body,etc) to prevent those coming in the generated output stream.

7.      Next, we need to set the Http MIME type of the output stream by setting ContentType property to “text/xml”. This can be either done in @Page directive or in code behind.

protected void Page_Load(object sender, EventArgs e)

    {

        this.Response.ContentType = "text/xml" ;

    }

OR

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RSS.aspx.cs" ContentType = "text/xml" Inherits="_Default" %>

8.      Execute the page; You can see the generated RSS feed.

 

Read my previous article, Generate RSS Feed in ASP.Net which discusses how to generate RSS feed using HttpHanlder.

 

Reference

Generate RSS Feed in ASP.Net

 

Downloads

Download Source 

Conclusion

Thus, we have understood implementation of one of the very famous and extremely useful Web 2.0 feature using Repeater control in ASP.Net 2.0. Download the source attached with this article to see RSS feed in action. To consume the RSS feed in our ASP.Net sites you can follow the article here.

Happy Coding!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments