RSS The Structure of an RSS Feed

Document Sample
RSS The Structure of an RSS Feed Powered By Docstoc
					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: <?xml version="1.0"?> <rss version="0.92" xml:base=""> ... </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 </rss> 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: <channel> <title>Your Title</title> <link></link> <description>Your XML RSS feed description</description> <language>en-us</language> <image> <title>Title of Image</title> <url></url> <link></link> <width>50</width> <height>44</height> </image> 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: 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=\"\">"); MyWriter.WriteLine("<channel>"); MyWriter.WriteLine("<title>My RSS Feed Title</title>"); MyWriter.WriteLine("<link></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></url>"); MyWriter.WriteLine("<link></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></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 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: 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 <item> 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 <rss version> line and looks like the following: <!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "">]> You would include it in your feed, as follows: <?xml version="1.0"?> <!DOCTYPE rss [<!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "">]> <rss version="0.92" xml:base=""> <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.