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.

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.

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.