Sunday, March 18, 2012

How to populate RSS Feed in .aspx

RSS is an XML format used for syndicating news and similar content from online news sources. It stands for "Really Simple Syndication" and consists of a channel that represents the news source, which has a title, link, and description that describe the news source minimally . Additionally, an RSS feed typically contains one or more item elements that represent individual news items, each of which should have a title, link, or description. Having an RSS feed for your site has become a necessity in recent years. With blogs or news sites being updated frequently, RSS has allowed readers to keep up with new content without being forced to visit them. This tutorial show you how to create an RSS feed with ASP.NET, and how to render XML documents on ASP.NET Web pages.

RSS Feed in .aspx

Create "RSSFeed.aspx" and remove default html markup and then put Repeater control and embed the RSS syntax as below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RSSFeed.aspx.cs" Inherits="WebAppBlog.RSSFeed" %>

<asp:repeater id="Repeater1" runat="server">
    <HeaderTemplate>
    <rss version="2.0">
      <channel>
        <title>Name of the Website</title>
        <link>http://www.yourdomain.com</link>
        <description>This is the syndication feed for yourdomain.com</description>
         <image>
          <title>Name of the Website</title>
          <url>your image url</url>
          <link>http://www.yourdomain.com</link>
        </image>
    </HeaderTemplate>
    <ItemTemplate>
        <item>
          <title><%# (Eval("CompanyName"))%></title>
          <description><![CDATA[<%# FormatForXML(Eval("ContactName") + " (" + Eval("ContactTitle") + ")" + Eval("Address"))  %>]]></description>
          <pubDate><%= DateTime.Now.ToString("R")%></pubDate>
          <link><%# "http://www.yourdomain.com/CompanyDetail.aspx?id=" + Eval("SupplierID")%></link>
        </item>
    </ItemTemplate>
    <FooterTemplate>
    </channel>
 </rss>
  </FooterTemplate>
</asp:repeater>

 I used the "Northwind" database (download here) and "Suppliers" table, and created ADO.NET Entity Data Model "NorthwindEntities" for data access (see here how to create Entity Data Model from Database).
After creating Entity Data Model, you will see "NorthwindEntities" connection look like below.
<connectionStrings>
     <add name="NorthwindEntities" connectionString="metadata=res://*/NorthwindModel.csdl|res:    //*/NorthwindModel.ssdl|res://*/NorthwindModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True&quot;"
         providerName="System.Data.EntityClient" />
</connectionStrings>

Add the following code to code-behind.

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!Page.IsPostBack)
        {
            using (var context = new NorthwindEntities())
            {
                var topProduct = context.Suppliers.OrderByDescending(p => p.SupplierID).Take(10).ToList();
                Repeater1.DataSource = topProduct;
                Repeater1.DataBind();
            }
        }
    }
    catch (Exception ex)
    {
    }
}
protected string FormatForXML(object input)
{
    string data = input.ToString();
    // replace those characters disallowed in XML documents
    data = data.Replace("&", "&amp;");
    data = data.Replace("\"", "&quot;");
    data = data.Replace("'", "&apos;");
    data = data.Replace("<", "&lt;");
    data = data.Replace(">", "&gt;");
    return data;
}

That's it. Now, your RSS Feed is available to your reader.

RSS 2.0 Specification

Similar Post

No comments:

Post a Comment