Geeks With Blogs

News
Employers
Soppa Group India
iSmart Panache Inc
R Systems Internationals Ltd
Technovate eSolutions Pvt Ltd
The contents of this blog are my personal opinion and do not represent in any way the view of my employer.
These postings are provided "AS IS" with no warranties, and confer no rights.

Google PR™ - Post your Page Rank with MyGooglePageRank.com

Narendra Tiwari

RSS (Really Simple Syndication) is a format for syndicating (organizing) news and the content of news-like sites, including major news sites, news-oriented community sites.

RSS-aware programs called news aggregators are popular in the weblogging community (blogs). Many weblogs make content available in RSS. A news aggregator can help you keep up with all your favorite weblogs by checking their RSS feeds and displaying new items from each of them.
references:
http://www.w3schools.com/rss/rss_intro.asp
http://www.xml.com/pub/a/2002/12/18/dive-into-xml.html

Free Rss APIs
Tortuga,
RSS.Net (An open-source .NET class library for RSS feeds)

RSS.Net Imlpementation: following is a .Net Generic Handler which generates the Rss feeds from Northwind database using Categories and Products tables this handler first renders the html table showing the categories, here each category link renders the Rss feeds for the products in that category.
RSS.Net Download the source code, compile the code and refer the dll Rss.Net.dll in following code.


<%@ WebHandler Language="C#" Class="rss" %>
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using Rss;

///
Rss Handler class provides the methods to get the Feed(s)
    public class rss :IHttpHandler
    {
        /// constructor
        public rss()
        {
        }

        #region Implementation of IHttpHandler
        public void ProcessRequest(System.Web.HttpContext context)
        {
           
//feedCode is mapped with CategoryId eg. 1,2
            int feedCode;
            if (! string.IsNullOrEmpty(context.Request.QueryString["feed"]))
            {
               
//This will make sure the page output will be text/XML and not HTML
                context.Response.ContentType = "text/xml";
                feedCode = int.Parse(context.Request.QueryString["feed"]);
                RenderFeed(context.Response.OutputStream, feedCode);
            }
           
else
            {
               
//get the distict Product List
                RenderFeeds(context.Response.Output);
            }
        }

        public bool IsReusable
        {
            get{return true;}
        }
        #endregion
        
      
  /// Generate the RSS Feed by fetching the values from Database
        private RssFeed CreateRssFeed(int feedCode)
        {
            RssFeed rssFeed = new RssFeed();
            DataSet ds = GetFeed(feedCode);
           
// Fill in our channel information:
            RssChannel channel = new RssChannel();
            channel.Title = ds.Tables[0].Rows[0]["ProductName"].ToString();
            System.Uri uri = new System.Uri(HttpContext.Current.Request.Url.AbsoluteUri);
            channel.Link = uri;
            channel.Description = ds.Tables[0].Rows[0]["UnitPrice"].ToString();
            channel.Generator = "Northwind";
            channel.Language = "en";
            channel.Copyright = "Copyright © " + System.DateTime.Now.Year + ", Northwind";
            channel.ManagingEditor = "Managing.Editor@Domain.com";
            channel.TimeToLive = 2;
            channel.PubDate = System.DateTime.Now;
           
// Add items.
            RssItem item;
            RssGuid rssGuid;
            RssSource rssSource;
            foreach (DataRow drow in ds.Tables[0].Rows)
            {
                item = new RssItem();
                item.Title = drow["ProductName"].ToString();
                item.Description = drow["QuantityPerUnit"].ToString();
                uri = new System.Uri(HttpContext.Current.Request.Url.AbsoluteUri + "&i=" + drow["ProductId"].ToString());
                item.Link = uri;
                rssGuid = new RssGuid();
                rssGuid.Name = drow["ProductId"].ToString();
                rssGuid.PermaLink = true;
                rssSource = new RssSource();
                rssSource.Name = "Northwind";
                uri = null;
                rssSource.Url = uri;
                item.PubDate = System.DateTime.Now;
               
//Add Item to channel
                channel.Items.Add(item);
            }
            rssFeed.Channels.Add(channel);
            return rssFeed;
        }
        
        /// Generate the Table with Distict Feed/CategoryId
        private void RenderFeeds(System.IO.TextWriter w)
        {
            DataSet ds = GetFeeds();

            System.Web.UI.WebControls.Table table = new System.Web.UI.WebControls.Table();
            System.Web.UI.WebControls.TableRow row;
            System.Web.UI.WebControls.TableHeaderRow hrow;
            System.Web.UI.WebControls.TableHeaderCell hcell;
            System.Web.UI.WebControls.TableCell cell;

            hrow = new TableHeaderRow();
            hcell = new TableHeaderCell();
            hcell.Text = "Category Id";
            hrow.Cells.Add(hcell);

            hcell = new TableHeaderCell();
            hcell.Text = "Description";
            hrow.Cells.Add(hcell);

            hcell = new TableHeaderCell();
            hcell.Text = "CategoryName";
            hrow.Cells.Add(hcell);

            table.Rows.Add(hrow);

            foreach (DataRow drow in ds.Tables[0].Rows)
            {
                row = new System.Web.UI.WebControls.TableRow();
               
//CategoryId
                cell = new System.Web.UI.WebControls.TableCell();
                cell.Text = drow["CategoryId"].ToString();
                row.Cells.Add(cell);
               
//Description
                cell = new System.Web.UI.WebControls.TableCell();
                cell.Text = drow["Description"].ToString();
                row.Cells.Add(cell);
               
//CategoryName
                cell = new System.Web.UI.WebControls.TableCell();
                cell.Text = "" + drow["CategoryName"].ToString() +
""
;
                row.Cells.Add(cell);
                table.Rows.Add(row);
            }

            HtmlTextWriter writer = new HtmlTextWriter(w);
            table.RenderControl(writer);
        }
        
        /// Render the RSS Feed on page
        private void RenderFeed(Stream stream, int feedCode)
        {
            CreateRssFeed(feedCode).Write(stream);
        }
        
        /// Returns the detail associated with the CategoryId
        public DataSet GetFeed(int feedCode)
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnNorthwind"].ToString()))
            {
                conn.Open();
                SqlDataAdapter adptr = new SqlDataAdapter("select * from Products Where categoryId = " + feedCode + "order by ProductId" , conn);
                adptr.Fill(ds);
            }
            return ds;
        }
        
        /// Returns the Distinct EventCodes Detail
        public DataSet GetFeeds()
        {
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnNorthwind"].ToString()))
            {
                conn.Open();
                SqlDataAdapter adptr = new SqlDataAdapter("select * from Categories Order By CategoryId", conn);
                adptr.Fill(ds);
            }
            return ds;
        }       
    }


../rss.ashx

../rss.ashx?feed=1

above code is written in VS 2005, so your connection string should look like :-
<connectionStrings>
            <
add name="cnnNorthwind" connectionString="Server=SERVER;Database=Northwind;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
connectionStrings>

Consuming RSS feeds: RssPopper is a news aggregator add-in for Outlook & Outlook Express. News items delivered directly to Outlook as e-mails.

Please mark your valuable comments and feedbacks.

Posted on Wednesday, February 8, 2006 10:26 AM .Net | Back to top


Comments on this post: Generate Rss Feeds from .Net generic Handler

# re: Generate Rss Feeds from .Net generic Handler
Requesting Gravatar...
Hii...i like the above article...i have refered to many articles on the Web for the RSS...but i m gettng problem in creating RSSFeed...i want to create RSS from my BlogItems.....can u please Help me...i want to create RSS like this site...www.perezhilton.com...i want to do same as shown in this site...i have blog section in which users can comment or reply...i want to give all this in the RSS...please help..i will thankfull to you if u provide me Example in ASP.NET(C#)
Left by Pushkar on Feb 29, 2008 10:40 AM

# re: Generate Rss Feeds from .Net generic Handler
Requesting Gravatar...
Hi I liked the article , thanks for writing about it I didn't even had a clue how to write it you saved my time and I'm greatful for that
Left by web development company on Aug 14, 2009 5:46 AM

Your comment:
 (will show your gravatar)


Copyright © Narendra Tiwari | Powered by: GeeksWithBlogs.net