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">
    <rss version="2.0">
        <title>Name of the Website</title>
        <description>This is the syndication feed for</description>
          <title>Name of the Website</title>
          <url>your image url</url>
          <title><%# (Eval("CompanyName"))%></title>
          <description><![CDATA[<%# FormatForXML(Eval("ContactName") + " (" + Eval("ContactTitle") + ")" + Eval("Address"))  %>]]></description>
          <pubDate><%= DateTime.Now.ToString("R")%></pubDate>
          <link><%# "" + Eval("SupplierID")%></link>

 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.
     <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" />

Add the following code to code-behind.

protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            using (var context = new NorthwindEntities())
                var topProduct = context.Suppliers.OrderByDescending(p => p.SupplierID).Take(10).ToList();
                Repeater1.DataSource = topProduct;
    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