Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider Follow Me On Twitter
Using the RSSToolkit to Consume an RSS Feed.

Last month I talked about using the RSSToolkit to produce an RSS feed for your site.  This month I am going to follow up on that by explaining how to consume an RSS feed into your web site or smart client application.  Again I am going to use the RSSToolkit.

You will need to create a page and drop a Data Control on it, in this example I am going to use the GridView.  I like working with templates, because it gives a little better control of the data on the page.  First add an EmptyDataTemplate to the structure and put your message in so you can tell the visitor you just do not have anything to read at this point from this feed.  Now add your Template Field with the layout you want.  Mine has two controls in it, one for the title and one for the entry description.  To reduce my coding and keep this simple I am going to do something I rarely do and that is inline data-binding.  This will add the actual data members to our controls.

                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None">
                        <EmptyDataTemplate>
                            Sorry there are no items for this feed available at this time.
                        </EmptyDataTemplate>
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <table border="0" cellpadding="0" cellspacing="0" width="500" align="center">
                                        <tr>
                                            <td align="left">
                                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("link") %>' Text='<%# Eval("title") %>'
                                                    Font-Bold="true" ForeColor="#339944"></asp:HyperLink></td>
                                        </tr>
                                        <tr>
                                            <td align="left">
                                                <asp:Literal ID="ltlDescription" runat="server" Text='<%# Eval("description") %>'></asp:Literal></td>
                                        </tr>
                                    </table>
                                    <br />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Now go to the code behind file and add something like the following to bind the feed data to the GridView:

    Private Sub Binddata()

        Dim c As GenericRssChannel = GenericRssChannel.LoadChannel("http://professionalaspnet.com/blogs/aspnet_blog/rss.aspx")

        GridView1.DataSource = c.SelectItems()
        GridView1.DataBind()

    End Sub

You can replace the value in the "LoadChannel" method with the URL of the feed you want to consume.  The second step is to bind the "SelectItems" to the GridView and call the DataBind method to bind the RSS feed to your grid.  That is all there is to it.

 

Posted: Sunday, August 20, 2006 3:22 PM

by Chris Love
Filed under: , ,

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS