Tuesday, November 4, 2008

RSS using Repeater control

All of a sudden, I got craze of RSS and googled for some fine articles. Though got some fine ones, I’s looking for some professional ones. Alas, I got one from MSDN, using the Repeater Control.

Ok! I’ll tell you how to implement. Just follow me.

Step 1: Create an ASP.NET website

Step 2: Add a page names, RSS.aspx.

Step 3: Now add a Repeater control to RSS.aspx.

Now, switch to the source-view of RSS.aspx and clear all the tags except <%@ Page%> tag.

RSS.aspx (source-view)

   1: <%@ Page Language="C#" ContentType="text/xml" AutoEventWireup="true" CodeFile="RSS.aspx.cs" Inherits="RSS" %>
   2:  
   3: <asp:repeater ID="Repeater1" runat="server">
   4:     <HeaderTemplate>
   5:         <rss version="2.0">
   6:             <channel>
   7:                 <title>ASP.NET News</title>
   8:                 <link>http://www.aspnetnews.com/headlines/</link>
   9:                 <description>This is the syndication feed foe aspnetnews.com</description>
  10:     </HeaderTemplate>
  11:     <ItemTemplate>
  12:         <item>
  13:             <title><%
   1: # FormatForXML(DataBinder.Eval(Container.DataItem, "title")) 
%></title>
  14:             <description><%
   1: # FormatForXML(DataBinder.Eval(Container.DataItem, "Description")) 
%></description>
  15:             <link>http://www.aspnetnes.com/story.aspx?id=<%
   1: # DataBinder.Eval(Container.DataItem, "ArticleID") 
%></link>
  16:             <author><%
   1: # FormatForXML(DataBinder.Eval(Container.DataItem, "Author")) 
%></author>
  17:             <pubDate><%
   1: # String.Format("{0:R}", DataBinder.Eval(Container.DataItem, "DatePublished")) 
%></pubDate>
  18:         </item>
  19:     </ItemTemplate>
  20:     <FooterTemplate>
  21:             </channel>
  22:         </rss>
  23:     </FooterTemplate>
  24: </asp:repeater>




RSS.aspx (code-behind)




   1: using System.Data.SqlClient;
   2:  
   3: protected void Page_Load(object sender, EventArgs e)
   4:     {
   5:         string provider = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=.\sqlexpress";
   6:         string sql = "SELECT TOP 5 ArticleID, Title, Author, Description, DatePublished FROM Article ORDER BY DatePublished DESC";
   7:  
   8:         SqlConnection connection = new SqlConnection(provider);
   9:         SqlCommand command = new SqlCommand(sql, connection);
  10:  
  11:         connection.Open();
  12:  
  13:         Repeater1.DataSource = command.ExecuteReader();
  14:         Repeater1.DataBind();
  15:  
  16:         connection.Close();
  17:     }
  18:  
  19: protected string FormatForXML(object input)
  20:     {
  21:         string data = input.ToString();
  22:         data = data.Replace("&", "&amp;");
  23:         data = data.Replace("\"", "&quot;");
  24:         data = data.Replace("'", "&apos;");
  25:         data = data.Replace("<", "&lt;");
  26:         data = data.Replace(">", "&gt;;");
  27:  
  28:         return data;
  29:     }




Table Structure



1 comment:

Anonymous said...

What about table structure. Is it still incomplete ?

 
Best viewed in Internet Explorer 8.