Create RSS feed programatically from data in C#

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();
}

23 comment(S)


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

sponsored

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

Jessica on Sep 21, 2010 07:05 AM

Muy buen ejemplo, muchas gracias, me ha sido de gran utilidad!!!

Ted on Oct 20, 2010 10:11 AM

I tried your example and it works great. However, i have one issue. When i subscribe to the feed it only shows the first article. It does this in both IE8 and outlook. How do i get the feed to show the older articles?

Vijai Prakash Maurya on Dec 15, 2010 09:26 AM

hi,

its really very good logic, but i am also confused about this line.

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


Regards
Vijai P Maurya

Simo on Jan 19, 2011 03:21 AM

Excellent! Thank you for sharing this!

Ashok on Apr 7, 2011 03:03 AM

while i am adding
PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

line that time error shows "the type of namespacename PostList not found ".

how can i resolve that error.

please can u explain it me.

sama on May 16, 2011 06:27 PM

while i am adding
PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

line that time error shows "the type of namespacename PostList not found ".

how can i resolve that error.

please can u explain it me.

please reply me

Tony on Aug 15, 2011 10:33 AM

The PostList object that everyone keeps asking about is his collection of posts. You need to modify this to work for you. Whether you are using LINQ or standard data connections, you need to modify that line to be your collection of posts. That line of code will only work in his application and needs to be customized for your own.

Nilesh on Nov 14, 2011 10:30 PM

hi i am a web developer currently working on indiainfoline.com we have rss feed section but the problem is that it does not get updateed automatically, i ned to update it when new data is inserted in database any idea.

vishnuvardhan on Dec 23, 2011 08:23 PM

this is useful for colecting more information
please any one send rss feed codeing

vishnuvardhan on Dec 23, 2011 08:24 PM

this is useful for colecting more information
please any one send rss feed codeing

Raship on Jul 12, 2012 12:16 AM

How to display image in rss feed??

william on Dec 27, 2013 09:55 PM

don't know how to get full-text content

lilycan on Apr 9, 2014 08:36 PM

the followiing solution might help you solve the problem of displaying image, have a test.
http://www.rasteredg....view-operate-online/

Leave a comment