Search 
DailyCoding > Web

Create RSS feed programatically from data in C#

This article will explain how can you to create you own utility to generate rss feed from yous data programatically
Author admin on May 24, 2008 4 Comments
Rate it    (Rated 3 by 15 people)
4,189 Views

I haven’t used any built in application or framework to development DailyCoding.com. All the code has been written from scratch by me. It is not a big site, but I keep on adding things as they are needed. I don’t know any tool which could automatically generate RSS feed from your won data, so I decided to write my own code. Here is the code which will generate RSS feed for most recent post in this web site.

protected void Page_Load(object sender, EventArgs e)
{
  // Clear any previous output from the buffer
  Response.Clear();
  Response.ContentType = "text/xml";
  XmlTextWriter feedWriter 
    = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

  feedWriter.WriteStartDocument();

  // These are RSS Tags
  feedWriter.WriteStartElement("rss");
  feedWriter.WriteAttributeString("version", "2.0");

  feedWriter.WriteStartElement("channel");
  feedWriter.WriteElementString("title", "Daily Coding");
  feedWriter.WriteElementString("link", "http://www.dailycoding.com");
  feedWriter.WriteElementString("description", "Daily Coding");
  feedWriter.WriteElementString("copyright", 
    "Copyright 2008 dailycoding.com. All rights reserved.");
 
  // Get list of 20 most recent posts
  PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

  // Write all Posts in the rss feed
  foreach(PostInfo post in posts)
  {
    feedWriter.WriteStartElement("item");
    feedWriter.WriteElementString("title", post.Title);
    feedWriter.WriteElementString("description", post.PostHtml);
    feedWriter.WriteElementString("link", 
      UrlHelper.GetShowPostUrl(this, post.Name));
    feedWriter.WriteElementString("pubDate",
      post.DatePosted.ToString());
    feedWriter.WriteEndElement();
  }

  // Close all open tags tags
  feedWriter.WriteEndElement();
  feedWriter.WriteEndElement();
  feedWriter.WriteEndDocument();  
  feedWriter.Flush();
  feedWriter.Close();

  Response.End();
}
ASP.NET | Data | Utility

Discussion

Sam On May 26, 2008 09:18 PM
Wow, I did the same thing but your version is better :) bookmarked! Thanks a lot.

n On Aug 21, 2008 03:53 AM
nmhj,m,

Aron On Dec 26, 2008 07:50 AM
You should include the required "includes"


eg:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

Luís Soares On Jan 27, 2009 08:45 AM
Another solution is to use WCF (Windows Communication Foundation)

This way one can easily create a feed in RSS or Atom:
http://msdn.microsof....ibrary/bb412174.aspx

Leave a Comment

Name
Email Address
Web Site
© Copyright 2008 Daily Coding • All rights reserved