An RSS feed is really just a fancy term for an XML file, specifically used to re-distribute web content to aggregators. The most commonly used form of an RSS feed comes from blogs, which are constantly increasing in popularity. Combine the interest in blogs with the major web browsers’ ability to now aggregate RSS feeds, and you have a very prominent reason to start creating RSS feeds in your C# ASP .NET web application.
There are actually two types of popular feeds, Atom and RSS. Atom feeds are a more reader-friendly format of XML, and typically display in the web browser in a readable format by simply navigating to the feed.xml URL. RSS feeds, on the other hand, are meant as a more stripped-down version of XML, more easily parsed by aggregators. When viewing an RSS XML feed in your web browser, you will see a list of data tree branches, where you can shrink and expand the XML sections. Internet Explorer 7.0 and Firefox have specific features to read RSS feeds much more easily than prior versions.
In the sample code that follows, we will be creating an RSS formatted XML feed, which can be easily aggreagated by software programs and robots.
Creating an RSS feed in c# .NET is actually pretty easy. It involves generating an RSS header, body content, and an RSS footer.
The header of an RSS feed generally looks as follows:
The next section would include the Channel data, followed by the article content (also called items). The RSS feed is closed with a simple tag. The RSS feed channel described the summary of what the RSS feed contains and where the data comes from. The channel section has the following format:
Notice the channel includes a section to describe an associated image. This is important, as most RSS aggregators will display the image at the top of the feed’s data, which makes the content more readable and identifies a brand.
To create an RSS feed in C#, you use the FileStream object to create the physical file and output the neccessary XML header and data. First, a file is created and opened, just as you would do when writing data to a file on disk. The only difference is that the file extension will be .xml or .rss. Most aggregators prefer .xml file extensions.
An example of generating the RSS feed header is shown below:
There are several important items to note in the above code. The most important is that the FileStream and StreamWriter variables are defined above the try block, opened inside the try block, and cleaned up and closed in the finally block. This ensures all file handles are properly dealt with even under an error condition.
A second note is the section where article content (items) are written to the feed. You can add as many item sections as you wish. However, most aggregators will only recognize the first 10 item entries.
Notice in the item described above, there is HTML content enclosed within a CDATA tag. The CDATA tag is important in that it allows you to use HTML tags inside the RSS feed content. The CDATA tag is specified by a starting tag of: <![CDATA[
The CDATA tag is ended with: ]]>
It is easy to confuse the start and end tag characters, so when using HTML within your RSS feed, be sure to properly format the CDATA tag.
Another catch with generating an RSS feed is the published date field. This field must be in GMT format. This can easily be achieved in C# ASP .NET by simply casting the DateTime object to be printed in “r” format via System.DataTime.Now.ToString(“r”).
An example of a properly formatted PubDate entry in GMT format would be:
Fri, 27 Oct 2006 03:17:20 GMT
The basics of creating an RSS feed in C# .NET are shown above. However, to truly take advantage of the power of RSS and XML, you should automatically generate the feed, on the fly, from your content.
An example of auto-generating RSS content are the articles on this site. Each time a new article is published, the C# .NET web application automatically re-generates an RSS feed, containing an excerpt of each article and linking to the original article content. By looping over the articles in the MSSQL database and creating the .xml file, an RSS feed is instantly generated.
An interesting tactic when creating RSS feed item content is to only include an excerpt of the original article’s content for distribution in the feed. The idea is to give a tease of the content to the reader, causing him to click the link to follow through to your site. After all, typically, the ultimate goal of creating RSS feeds are to lure more traffic to your web site.
You can easily truncate article content, for use within an RSS feed, by using the SubString() method of the string object. For example:
You would then store the teaser within the
Typically, many RSS feeds include a DTD tag which specifies how a feed should be viewed. A DTD tag is included in the header just before the
You would include it in your feed, as follows:
It is important to note that Internet Explorer 7.0 does not support DTD feeds. Rather than simply ignoring the DTD line and showing the feed as-is, IE 7 insists on displaying an error. With the popularity of Internet Explorer, it is important to have your RSS feed viewable in the web browser. Therefore, it is preferable to leave out any DTD line.
Creating RSS feeds, manually or automatically, can create increased demand and availibility of your web site’s content. RSS feeds are becoming an important part of every web site. Generating compliant XML feeds in C# ASP .NET is an easy task. By manipulating the FileStream, StreamWriter, and DateTime objects, you can quickly create RSS or Atom compatible XML feeds for use within your own C# ASP .NET web site.
This article was written by Kory Becker, software developer and architect, skilled in a range of technologies, including web application development, machine learning, artificial intelligence, and data science.