Monday, September 04, 2006

Generate RSS Feeds with ASP.NET and LINQ to XML

Jim Wooley has written a clever LINQ to XML procedure that generates RSS feeds from event logs. If you've installed the Microsoft Visual Studio Code Name “Orcas” Language-Integrated Query, May 2006 Community Technology Preview, you can download Jim's source code for the _Default partial class as Default.aspx.vb, and give it a test drive, preferably with IE 7.0 RC1 or later. Just create a new Visual Basic LINQ ASP.Net Web Site from the VB templates and substitute his Default.aspx.vb for the original empty version under the Default.aspx node, as shown here:

Tip: Clear at least the Application log and reboot before running the project. Otherwise, you'll find that Iexplore.exe will consume almost all available memory while it attempts to generate an RSS file with several thousand items.

Press F5 to build and run the project. If you have IE 7.0 RC1 or a late beta, the page appears as shown here:

If you're familiar with Dave Winer's RSS 2.0 spec, you'll notice some missing items—such as the top-level <rss version="2.0"> element—and non-conforming <publisher> and <author> tags under the <channel> element. <item> elements also have non-conforming <date> and <creator> tags, and dates that don't conform to RSS's requirement for the RFC 822 date/time format.

Adapting the Page for Feed Readers

IE 7.0 ordinarily displays RSS and ATOM files with a style sheet. For example, here's the start of the IE 7.0 feed page for the OakLeaf Systems blog as of this post's date:

Note: IE 7.0's style sheet treats ATOM 0.3/1.0 feeds as RSS 2.0 feeds with elements qualified by ATOM namespaces.

I wanted to see how IE 7.0's style sheet would handle the feeds generated by Jim's project. The missing top-level <rss version="2.0"> element prevents IE 7.0 from applying its feed style sheet. I believe that other non-conforming elements might prevent some readers from displaying the data correctly. I also wanted to add hierarchical <category> tags to see if IE 7.0 would filter events by category correctly.

So I added the <?xml version='1.0' encoding='UTF-8'?> XML declaration and the <rss version="2.0"> element pair, then made other minor corrections and a few additions to bring the feed up to the RSS 2.0 standard. Here's the result before subscribing to the feed in IE 7.0:

Note: The five filter categories are EventLog, EventLog/Entries, EventLog/Entries/Application, EventLog/Entries/Information, EventLog/Entries/Application/Warning. Filtering works as expected.

And here's the transformed page after subscribing to the feed:

If you publish the Website to IIS, run Cassini on a fixed port, or just copy the current URL—http://localhost:1686/XLinqRSS/Default.aspx—for this example you can subscribe to the feed with any popular reader. For example, here's Dare Obasanjo's RSS Bandit open-source reader displaying the OAKLEAF-MS15 feed:

You can read an abbreviated version of the updated RSS 2.0 feed code here and see the modified Visual Basic 9.0 source code for the updated WriteRSS20 subprocedure here.

Syndicating event logs isn't a mainstream RSS application, but Jim Wooley's technique demonstrates the remarkable efficiency of programming with LINQ for XML and VB 9.0's XML inline literals with inserted element and attribute values.

Technorati Tags: LINQ to XML, XLinq, LINQ, Orcas, RSS 2.0, RSS, Event Logs, Jim Wooley

0 comments: