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 10 Comments
Rate it    (Rated 3 by 17 people)
8,148 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

suneel On Sep 16, 2009 10:10 AM
Hi,
It's really superb.
Do u hv any code 4 doing the same in ASP classic (vbscript).
Pls can anyone share it..

pedro On Sep 25, 2009 04:27 AM
wich library contains de postinfo class?

Luke On Oct 14, 2009 02:48 PM
The only problem with this is knowing what the elements etc are for an RSS feed. I found this website which has classes for all the feed options so you can do it using objects instead! http://tr3v.net/applications/dotrss

dd On Oct 22, 2009 09:39 AM
I recieve an error when I reach this point Response.ContentType = "text/xml";
It says contenttype cannot be changed once headers are sent.

gurubn On Apr 6, 2010 05:47 AM
Hello ,

I am really impressed with the logic . This is very structured but i am facing difficulty to understand the below line ,

PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

Which class "PostList " belongs to , is Dot net api or dll reference or 3rd party ?

THis will help me

Simon On Jun 16, 2010 07:17 AM
Hi,

PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

Is a collection of posts for his specific website. all this does is pull back all of the news posts (a collection) from the database.

Once we have this data we then loop through each post within the collection:

foreach(PostInfo post in posts)
{
Justs loops through each post.
}

Simon

Leave a Comment

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