Geeks With Blogs

News

  
Bill Evjen's Blog Code, Life and Community

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>
  <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:

 

<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 &amp; 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.

Posted on Friday, July 23, 2004 8:36 AM ASP.NET , Programming Languages | Back to top


Comments on this post: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Bill, I was trying to recreate this app myself (without cutting and pasting anything from your code here). I'm using Beta 1 that was made available at the August/September MSDN Events.

I created the sub as code-behind, rather than in-line, and it caused me no end of trouble!

I had an exact copy of your code running fine, but my version wouldn't refresh the DataList even though I ensured I had AutoPostBack enabled.

Any thoughts on why this would occur?
Left by Jonathan on Oct 19, 2004 10:22 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
You will need to construct your <asp:dropdownlist> as such:

<asp:DropDownList ID="DropDownList1" Runat="server" AutoPostBack="True"> ... </asp:DropDownList>

and the code behind will be like this:

Partial Class Default_aspx

Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
XmlDataSource1.DataFile = DropDownList1.SelectedValue
End Sub
End Class

Hope that helps.

Bill
Left by Bill Evjen on Oct 19, 2004 10:43 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
It there a way to write that back to the XmlDataSource ? i.e binding the xml file to treeview, adding a node to the treeview, and than bind the treeview to the xmldatasource ? so when I'll open the treeview next time, the new node will appear ?
Left by eli on Jan 12, 2005 9:54 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
After setting the DataFile on runtime, it wouldnt work if we give the ID of the XmlDataSource to GridView. GridView still shows no rows.
Left by Shafia on Mar 22, 2005 4:31 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Can you restrict the number of items shown when using a combination of XmlDataSource and DataList controls?

I'd like to set up a summary of the items in the rss feed on my home page, i.e. show the first three items only and then a 'more' link to the full rss feed page. BTW I'm new to ASP.NET 2, coming from ColdFusion MX. Thanks!
Left by Tony on Jul 22, 2005 1:00 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
I've set up your feed exactly as noted and it works fine when selecting something from the dropdown list. However, when the page is firstloaded, it does not have updated stories. If you select a topic from the list and then re-select the default topic - it updates the information. Any ideas?
Left by JMK on Oct 26, 2005 7:47 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
<h1>hi</h1>Nice tutorials
Left by jim on Jan 12, 2006 7:25 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Great article! One question though... how can I display a feed article in my website WITHOUT redirecting to the source website? For example, when I click the "title" it takes me to the page where the article originates from. Although I can open the new article in a seperate window, I would much rather keep the user from directing away from my site and also keep my formatting and styles for the content. I see many sites (such as Yahoo Sports and ESPN) that share the same exact news feeds and display them in their sites with their own formatting and styles. Any idea how this can be done? Thanks!
Left by Avalon on Jan 17, 2006 8:26 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Good article and very helpful! I'll use it for my site. Thanks!!!
Left by David on Jun 02, 2006 11:50 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Tony, you can restrict the number of items in the XPath expression of the XmlDataSource. For example to get 5 items use this: XPath="rss/channel/item [position()<=5]"
Left by Greg on Jun 09, 2006 9:05 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Excellent tutorial! Thanks for providing this information!
Left by Serissa R. Olvera on Jun 25, 2006 11:50 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Is there way to make it auto refresh??
Left by Tej on Mar 21, 2007 2:13 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Very useful article. I never new consuming RSS feed was so easy
Left by NKab on Jan 10, 2008 4:57 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
META HTTP-EQUIV=Refresh CONTENT='600; URL=index.php'>

Pay attention to the Content='600; and the URL=index.php'

those are the things you edit.

600 = the amount of seconds between refresh cycles. Change it to whatever you want.

index.php = the file you want to refresh. Change it to whatever page you want to refresh.

I have mine set to index.php to refresh my entire site every 10 minutes.
Left by vijayanand on Jan 24, 2008 2:41 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
Nice Article
Left by Utsav on Apr 02, 2008 6:17 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond "
every time i debug the application i will getting same type of error
please help
Left by dinesh kumar on Mar 05, 2009 5:20 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
i want to consume an rss feed that is created in the same appli8cation based on pudate using asp.net will u help me
Left by sini on Mar 17, 2009 12:33 AM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
It is very usable article, thanks for it. But I have a question:
Is there any way that doing the same thing but the difference is the dropdownlist elements are taken from the database and tranferred to the xmldatasource ?
Left by Yasemin on Apr 07, 2009 5:44 PM

# re: ASP.NET 2.0: Consuming the Reuters RSS Feeds using the XmlDataSource Control
Requesting Gravatar...
thanks for the brilliant article.
Left by macro on May 01, 2010 10:30 PM

Your comment:
 (will show your gravatar)


Copyright © Bill Evjen | Powered by: GeeksWithBlogs.net