|
Comments
|
Today's Top SOA Links
ASP.NET Using an RSS Feed as a Content Service in ASP.NET
Bind Web pages to XML just as to databases
By: Joe Stagner
Oct. 5, 2005 11:00 AM
In the case of our business problem we need to use RSS to provide up-to-date MSDN Events listings on any community Web site that will display them. Creating RSS feeds is a bit more complex than consuming them (we'll get to creating an RSS 2.0 feed in ASP.NET shortly.) Luckily, the creation of RSS feeds has become very common. A quick visit to www.MSDNEvents.com shows that an RSS feed of upcoming events is available. Note the common orange "RSS Button" signifying a feed link (see Figure 1). If we click on the button and take a quick look at the feed itself, we see the information contained in Listing 2. Note that in addition to information about the feed itself, each item has a title, a link, and a description. Our ASP.NET code will display the event title, which comprises the City, State, and Date of the event, and will display that information as a link object whose target is the link URL from the RSS feed item. In its simplest form, RSS processing is just XML processing. We could simply do the following:
This is one of the great powers of RSS, and it can be used very simply, but you can get far cleverer if you choose. While we could write this code from scratch, there is a rich collection of RSS handling source code already in the public domain. Our code will be based on Dariush Tasdighi's Code Project article on building a simple RSS3 library (www.codeproject.com/aspnet/Working_on_RSS_20.asp). For a more advanced RSS control sample I suggest you check out Scott Mitchell's RSSFeed control and GotDotNet.com workspace at http://gotdotnet.com/Workspaces/Workspace.aspx? id=6a116a0f-66c4-4f91-9e13-a0d00960b9ad. Our ASP.NET application defines an RSSUtilities class within a namespace called JoeOnDotNet.RSS, which contains the GetRSSFeed method. Our JoeOnDotNet namespace also defines classes for the Feed's Root object (RSSRoot), Channel object (RSSChannel), Images (RSSImage), Items (RSSItem), and a collection class to contain RSSItems (RSSItemCollection). GetRSSFeed creates an XMLText-Reader object that is parametrically defined by a location enumerator, a feed address, and a type enumerator. That XMLTextReader is then used to populate a table in a dataset. Once our RSS feed items are in a Dataset Table, we simple bind the data to the user interface object we want to use to display the data, in our case a datagrid. See default.aspx and default.aspx.cs (source code is available by viewing the article online at http://dotnet.sys-con.com/read/issue/archives/, Vol: 3, iss: 9). Notice that most of the code drives the selection of the RSS feed to use and that the actual table displayed from the RSS feed is just a little code. Our web page looks like Figure 2. By binding to a different RSS source the page displays a different set of items (see Figure 3). Figure 4 shows our sample application, which includes a drop-down list of different feed types to be used in our grid. Notice that the first four read an .xml file form the file system or via a URL, but the second two select a URL that points to one of our ASPX files. Since we already have our RSS namespace with all of the classes we need to work with, an RSS feed, and a collection of RSS items, it will be easy for us to create an RSS item collection manually as it was to populate a collection from a feed file. We simply need to create an RSSRoot object (see create_rss2_sample1.aspx), which contains Chanel, Images, and Item Collections objects, and then add RSSItems to the RSSRoot.RSSItemCollection. We then use the PublishRSS method of the RSSUtilities class to return our RSS XML by way of an XMLTextWriter. By now you're getting the picture. At its root RSS processing is an example of the power of XML. We have a known (RSS 2.0) schema and we've implemented a set of classes to retrieve XML that complies with the schema from any source (file, URL, etc.), and then iterates the XML according to the schema to create HTML content. As long as the origin of the content, in our case MSDNEvents.com, observes the schema, our Web site will always pull the latest items from the content publisher and we will never have to edit the containing Web sites. Thus we have a very simple and powerful content syndication service that uses RSS and ASP.NET. You're up and running, but you're just getting started. Moving forward you can explore the optional channel sub elements, using trackbacks, third-party enhancement modules, as well as writing your own custom extension modules. Enjoy the power of federated content using RSS and ASP.NET, and keep reading .NET Developers Journal. Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||