How to programmatically create RSS feed using C# and XmlTextWriter


Are you looking for sample code to generate RSS feeds using C# or VB.NET? Take a look at the provided examples to create RSS.

Recently I was trying to submit our technology website www.Techulator.com to Google News. I learnt that Google need a separate RSS Feed that provide the feeds of all posts that can be classified as "News". Since our regular RSS feed does not provide just the News posts, I decided to create a separate feed that include only the News items. Since this is a custom C# implementation in our ASP.NET project, I explored a bit and ended up writing a few lines of code.

Here is the code I used to generate RSS feed from our site:


Response.Clear();
Response.ContentType = "text/xml";

System.Xml.XmlTextWriter rssWriter = new
System.Xml.XmlTextWriter(Response.OutputStream, System.Text.Encoding.UTF8);

rssWriter.WriteStartDocument();

rssWriter.WriteStartElement("rss");
rssWriter.WriteAttributeString("version", "2.0");

rssWriter.WriteStartElement("channel");
rssWriter.WriteElementString("title", "Technology News");
rssWriter.WriteElementString("link", "http://www.dotnetspider.com");
rssWriter.WriteElementString("description", "Latest Technology News from dotnetspider.com");
rssWriter.WriteElementString("copyright", "Copyright 2012 dotnetspider.com.");

// Write all Posts from table to the rss feed
foreach (DataRow row in rssTable.Rows)
{
rssWriter.WriteStartElement("item");
rssWriter.WriteElementString("title", (string)row["Title"]);
rssWriter.WriteElementString("link",
"http://www.dotnetspider.com/resources/ViewResource.aspx?Id=" +
(int)row["Id"]);
rssWriter.WriteElementString("description", (string)row["Description"]);
rssWriter.WriteElementString("author", (string)row["Name"]);
rssWriter.WriteElementString("pubDate",
((DateTime)row["PostedDate"]).ToString("r"));
rssWriter.WriteEndElement();
}

// Close xml elements
rssWriter.WriteEndElement();
rssWriter.WriteEndElement();
rssWriter.WriteEndDocument();
rssWriter.Flush();
rssWriter.Close();

Response.End();


The above code is very simple and straight forward. Let us look at the code.

Response.Clear();
Response.ContentType = "text/xml";

The above 2 lines clear everything in the Response so that we can push the RSS XML into the Response object.

In the next step, we initialize XmlTextWriter object and pass the Response Stream so that it can create the RSS XML and fill the Response stream. You can see that we are iterating through a database table which contains the records representing the News items.

When the above code is executed, the output will be the RSS feeds. There will be no webpage displayed, instead, you can see the valid XML file.

See a live implementation of this code in Techulator:
http://www.techulator.com/rss/resources.aspx. Learn more about RSS Feeds in Wikipedia.

You can download the full .cs page by clicking the download link below. If you are looking for sample source code to generate RSS using VB.NET, you may simply convert the below code in to VB.NET using our C# to VB.NET code coverter.


Attachments

  • RSSExample (44205-64927-RSSExample.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: