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):
<?xml version="1.0" encoding="UTF-8" ?>
<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>
<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§ion=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§ion=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:
<rss>
<channel>
<item>
</item>
</channel>
</rss>
We are going to want to get at everything that is contained in the multiple <item> 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
XmlDataSource1.DataFile = DropDownList1.SelectedValue
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Reuters News</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span style="font-size: 10pt; font-family: Verdana">Select News Topic:<br />
<asp:DropDownList ID="DropDownList1" Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="http://www.microsite.reuters.com/rss/topNews">Top News</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/businessNews">Business News</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/domesticNews">U.S. News</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/worldNews">International News</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/ElectionCoverage">Politics</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/Entertainment">Entertainment</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/technologyNews">Technology</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/scienceNews">Science</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/sportsNews">Sports</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/healthNews">Health</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/oddlyEnoughNews">Oddly Enough</asp:ListItem>
<asp:ListItem Value="http://www.microsite.reuters.com/rss/lifeAndLeisureNews">Life & Leisure</asp:ListItem>
</asp:DropDownList>
</span>
<br />
<br />
<asp:DataList ID="DataList1" Runat="server" DataSourceID="XmlDataSource1"
BorderColor="Tan" BackColor="LightGoldenrodYellow" BorderWidth="1px"
CellPadding="2" ForeColor="Black" Width="600px">
<ItemTemplate>
<asp:Label ID="Label1" Runat="server" Text='<%# XPath("pubDate") %>' ForeColor="gray" Font-Bold="True"
Font-Names="Verdana" Font-Size="XX-Small"></asp:Label><br />
<asp:HyperLink ID="HyperLink1" Runat="server" Text='<%# XPath("title") %>' NavigateUrl='<%# XPath("link") %>'
Target="_blank" Font-Names="Verdana" Font-Size="X-Small"></asp:HyperLink><br />
<%# XPath("description") %>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="Label3" Runat="server" Text='<%# XPath("pubDate") %>'
ForeColor="gray" Font-Bold="True"
Font-Names="Verdana" Font-Size="XX-Small"></asp:Label><br />
<asp:HyperLink ID="HyperLink2" Runat="server"
Text='<%# XPath("title") %>' NavigateUrl='<%# XPath("link") %>'
Target="_blank" Font-Names="Verdana" Font-Size="X-Small"></asp:HyperLink><br />
<%# XPath("description") %>
</AlternatingItemTemplate>
<AlternatingItemStyle BackColor="PaleGoldenrod"></AlternatingItemStyle>
<ItemStyle Font-Names="Verdana" Font-Size="X-Small"></ItemStyle>
</asp:DataList>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server"
DataFile="http://www.microsite.reuters.com/rss/topNews"
XPath="rss/channel/item">
</asp:XmlDataSource></div>
</form>
</body>
</html>
In this page, the DropDownList control contains multiple Reuters RSS feed destinations and the one selected by the end-user will be driving the content of the DataList control. I express the RSS information in two different ways. The first is by using ASP.NET server controls and specifying the RSS information from the Text property of the control as such:
Text='<%# XPath("pubDate") %>'
Or the NavigateUrl attribute of the HyperLink control as such:
NavigateUrl='<%# XPath("link") %>'
You can also express the information retrieved via the XmlDataSource control without a server control by using an expression as shown here:
<%# XPath("description") %>
Running this page will produce the following results (sorry for the quality of the images):

From this page, the end-user can select a category from the DropDownList and have it change the contents of what is displayed in the DataList control. This is done by enabling the AutoPostBack feature of the DropDownList control and using the SelectedIndexChanged event to reset the XmlDataSource control’s DataFile attribute.
Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
XmlDataSource1.DataFile = DropDownList1.SelectedValue
End Sub
You also don’t have to grab everything from the RSS feed. For instance, you might also want to just put a small list of headlines from the technology sector on your page and nothing else.
Hopefully with this little write-up, you can see some of the new and wonderful capabilities which ASP.NET 2.0 will provide us. This is just one example. ASP.NET 2.0 is packed with new capabilities and is only a download away.