ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control

Earlier I posted about the new Reuters RSS feeds that were made available on Reuters.com recently. These are news feeds which provide news headlines by category as well as the first sentence of the story and a link to the story as it appears on the Reuters.com website.

I decided to play with them myself using ASP.NET 2.0 and I thought that I would share with you the details on how to use these RSS feeds with the DataList and XmlDataSource controls.

The XmlDataSource control is an outstanding new addition to ASP.NET 2.0 which allows you to use not only XML files as an underlying data store, but to use streams of XML as the data store as well. This is quite ideal in the request/response world of ASP.NET.

Taking a look at the Top News feed from the Reuters RSS feeds, it will appear something like this (only a partial view):

  <rss version="2.0">

  <channel>

  <title>Reuters: Top News</title>

  <link>http://www.reuters.com</link>

  <description>Reuters: Top News</description>

  <image>

  <title>Reuters News</title>

  <width>120</width>

  <height>35</height>

  <link>http://www.reuters.com</link>

  <url>http://wwwi.reuters.com/comX/images/reuters120.gif</url>

  </image>

  <item>

  <title>FBI Probes Report Group Planning Convention Attack</title>

  <guid isPermaLink="false">5758968</guid>

  <link>http://www.reuters.com/newsArticle.jhtml?type=topNews&storyID=5758968&src=rss/topNews&section=news</link>

  <pubDate>Fri, 23 Jul 2004 15:02:15 GMT</pubDate>

  <description>BOSTON (Reuters) - The FBI said on Friday it was probing "unconfirmed information" that a domestic group is planning to disrupt next week's Democratic National Convention by attacking media vehicles with explosives or incendiary devices.</description>

  </item>

  <item>

  <title>U.S. Targets Militants in Raid on Iraqi City</title>

  <guid isPermaLink="false">5759122</guid>

  <link>http://www.reuters.com/newsArticle.jhtml?type=topNews&storyID=5759122&src=rss/topNews&section=news</link>

  <pubDate>Fri, 23 Jul 2004 15:26:10 GMT</pubDate>

  <description>BAGHDAD, Iraq (Reuters) - U.S. forces mounted an air strike on the rebellious city of Falluja Friday, the latest in a series targeting suspected insurgents linked to Jordanian militant Abu Musab al-Zarqawi, the U.S. military said.</description>

  </item>

The first step in building your ASP.NET 2.0 page which will consume this feed is to place an XmlDataSource control on your page. It will appear as a gray box on the design surface. Hover your mouse over the XmlDataSource control and an arrow will appear on the right side of the control. Pressing the arrow, you will find a link that will allow you to configure the control. Select this option.

This will bring up the Configure XmlDataSource dialog.

You can completely configure the control from here. For connecting to an RSS feed, you will need to use the DataFile attribute of the XmlDataSource control. This should be set to the link of the RSS feed source. In this case, I set it to the Top News option of http://www.microsite.reuters.com/rss/topNews. The other attribute to set is the XPath attribute. This value takes the XPath expression which will be used. In this case, we are going to want the value to be rss/channel/item. This is because the RSS feed is an XML document that is nested as such:

   

     

      

   

We are going to want to get at everything that is contained in the multiple node entries for this example.

Once these two attributes of the XmlDataSource control are set, let’s now turn our attention to some additional controls that will be needed on the page. For this example, use both a DropDownList control and a DataList control. In the end your page should appear as such:

<%@ Page Language="VB" %>

This article is part of the GWB Archives. Original Author: Bill Evjen

New on Geeks with Blogs