This post originated from an RSS feed registered with .NET Buzz
by Duncan Mackenzie.
Original Post: The article continued...
Feed Title: Code/Tea/Etc...
Feed URL: /msdnerror.htm?aspxerrorpath=/duncanma/rss.aspx
Feed Description: Duncan is the Visual Basic Content Strategist at MSDN, the editor of the Visual Basic Developer Center (http://msdn.microsoft.com/vbasic), and the author of the "Coding 4 Fun" column on MSDN (http://msdn.microsoft.com/vbasic/using/columns/code4fun/default.aspx). While typically Visual Basic focused, his blogs sometimes wanders off of the technical path and into various musing of his troubled mind.
Continuing along with the rest of the article, I start to layout the basic design of what I am about to build...
Designing our New Feature
As a fairly low-priority item, the request wasn't described in any more detail than that one line, but I had a pretty good idea about what folks around MSDN were looking for so I started with a rough design.
The RSS Viewer window will support the viewing of an RSS feed and the dragging of links from the feed into the rest of the system. The user will be able to select from a 'master list' of feeds that are exposed to all of the system users or from their own personal list, and they will have the option to enter a feed URL directly (adding it to their personal list if desired).
I couldn't think of a really simple way to display a feed (with variable size content, XHTML support, etc.) inside a DataGrid control, not without a bit of custom code, so I decided to display the feed using the Web Browser control (hosting IE on my Windows Form) instead. Using an XSL transform, I can convert the RSS information into HTML and then pass that HTML into the browser control for display.
Note A nice side-effect of this implementation decision is that I get the drag-and-drop functionality for free; the Web Browser control supports dragging links out of it and into other applications without any additional code.
The master list of links will be stored into some form of central repository, but the user's personal set of links will be stored locally and updated as needed. With some local preference editing needed, the final list of functionality is as follows:
Retrieve Master list of feeds from a central store
Retrieve Personal list of feeds from a local store
Retrieve and Transform a RSS feed into HTML
Display HTML in a Web Browser
Allow the user to enter in the URL for an RSS feed
Allow the user to add/edit/delete from their personal store of feeds
Allow the user to add/edit/delete from the Master list of feeds (note that this is certainly not suitable in all situations)
In my particular case, I am going to store the Master feed list into the same shared database being used by the main Page Planner system and then use the SqlClient classes to retrieve/edit it, but you might need a different implementation (Web Service based, file based central storage, etc.). To make it easy to swap out my particular method of storing the master and personal feed lists, I've designed those aspects of the program to be slightly abstracted through an IFeedStore interface that you can implement to create your own method of storing and retrieving the list of feeds.
Displaying the Feed
Of course, storing and updating feed lists is truly secondary to the purpose of this little project, the main functionality is to retrieve and display an RSS feed so it is best if I start by showing you that piece of the system.
To load the feed itself, I use the Load method of an XMLDocument. I then load up the XSL from another URL (or file location) into an XSLTransform instance. Finally, I use the Transform method of the XSLTransform class to take the XMLDocument and transform it using the XSL. The output from the transform is written into a stream, so I created a String based stream (an instance of IO.StringWriter) to accept the results.
Dim myDoc As New XmlDocument
myDoc.Load(rssURL.Text)
Dim result As New System.Text.StringBuilder
Dim resultStream = New IO.StringWriter(result)
Dim xslt As New XslTransform
xslt.Load(xsltURL.Text)
xslt.Transform(myDoc, New Xsl.XsltArgumentList, resultStream)
So far, this is really straightforward code, as the real work is being done in the XSL file itself. This XSL isn't capable of handling any RSS feed, as consistency isn't one of the strong-points of RSS implementations, but it has worked on the feeds from weblogs.asp.net, MSDN and GotDotNet so it should be sufficient for now.
Before I can continue with the article I really need to finish up the code, which I haven't had quite enough time for yet... so far I ended up creating a little 'test' application (see the pic below) that will eventually be scrapped in favour of the real system, and the same goes for that code I've pasted in above... but it is a start. The XSLT will likely be improved a bit more as well, it can't handle Chris Sells's feed at at the moment among others (due to the wonders of RSS and my own lack of knowledge about all of the different possible elements).