Creating XML RSS Feeds with C# ASP .NET

modified

Introduction

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.

Atom vs RSS

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.

The Structure of an RSS Feed

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:

1
2
3
4
<?xml version="1.0"?>
<rss version="0.92" xml:base="http://www.primaryobjects.com">
...
</rss>

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:

1
2
3
4
5
6
7
8
9
10
11
12
<channel>
<title>Your Title</title>
<link>http://www.yoursite.com</link>
<description>Your XML RSS feed description</description>
<language>en-us</language>
<image>
<title>Title of Image</title>
<url>http://www.yoursite.com/images/image.gif</url>
<link>http://www.yoursite.com</link>
<width>50</width>
<height>44</height>
</image>

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.

Generating an RSS Feed with C# ASP .NET

Generating RSS Feeds in C# ASP .NET

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
FileStream MyFileStream = null;
StreamWriter MyWriter = null;

try
{
   MyFileStream = new FileStream(Server.MapPath(".") + "\\myfeed.xml", System.IO.FileMode.Create,

   System.IO.FileAccess.Write);
   MyWriter = new StreamWriter(MyFileStream);

   // Write header of RSS file.
   MyWriter.WriteLine("<?xml version=\"1.0\"?>");
   MyWriter.WriteLine("<rss version=\"0.92\" xml:base=\"http://www.primaryobjects.com\">");
   MyWriter.WriteLine("<channel>");
   MyWriter.WriteLine("<title>My RSS Feed Title</title>");
   MyWriter.WriteLine("<link>http://www.yoursite.com</link>");
   MyWriter.WriteLine("<description>My feed description</description>");
   MyWriter.WriteLine("<language>en-us</language>");
   MyWriter.WriteLine("<image>");
   MyWriter.WriteLine("<title>My Title</title>");    

   MyWriter.WriteLine("<url>http://www.yoursite.com/image.gif</url>");
   MyWriter.WriteLine("<link>http://www.yoursite.com</link>");
   MyWriter.WriteLine("<width>50</width>");
   MyWriter.WriteLine("<height>44</height>");
   MyWriter.WriteLine("</image>");
   // Done adding header of RSS file.

   // Begin adding items (article content).
   MyWriter.WriteLine("<item>");
   MyWriter.WriteLine("<title>Title of article</title>");
   MyWriter.WriteLine("<link>http://www.yoursite.com/location_of_article_content.html</link>");
   MyWriter.WriteLine("<description><![CDATA[Body of content goes here.<BR>HTML text can be included inside this tag.<BR>]]></description>");
   MyWriter.WriteLine("<pubDate>" + System.DateTime.Now.ToString("r") + "</pubDate>");
   MyWriter.WriteLine("</item>");
   // End adding item (you can continue adding items here).

   // Write footer of RSS feed.
   MyWriter.WriteLine("</channel>");
   MyWriter.WriteLine("</rss>");
}
catch (Exception excep)
{
   Debug.WriteLine("Error: " + exception.Message);
}
finally
{
   MyWriter.Close();
   MyFileStream.Close();
}

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.

Using HTML in the Content of an Item

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.

Formatting the Published Date of an RSS Feed

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

Automatic Generation of RSS Feeds from C# ASP .NET

Automatic generation of RSS Feeds in C# ASP .NET

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.

Give Them a Taste, Not the Whole Thing

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:

1
2
3
4
5
if (FullArticle.Length > 1500)
{
   // Truncate the article content at 1,500 characters.
   ItemContent = FullArticle.Mid(0, 1500);
}

You would then store the teaser within the block of the RSS feed.

DTD Tags and Internet Explorer 7 Compatibility

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 line and looks like the following:

1
<!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">]>

You would include it in your feed, as follows:

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent">]>
<rss version="0.92" xml:base="http://www.primaryobjects.com">
<channel>
<item>
...
</item>
</channel>
</rss>

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.

Summary

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.

About the Author

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.

Share