5,316,172 members and growing! (20,410 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate License: The Code Project Open License (CPOL)

How to bind RSS feeds to a GridView

By fstrahberger

How to bind RSS feeds to a GridView.
VB, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 7 Nov 2006
Updated: 8 Nov 2006
Views: 16,870
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.59 Rating: 3.33 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
1 vote, 33.3%
5

Sample Image - GridView_RSS.jpg

Introduction

RSS-Feeds are great to stay informed about recent changes in your favorite websites. But it is quiet hard to read the XML documents for humans. So, I will show how you can bind RSS feeds (for example, the newest articles on CodeProject) to a GridView.

The GridView

First, create a new aspx file and drop a GridView to the form. Next, turn off "Auto generate fields" from the GridView properties. Last, create a Template field for the hyperlink to the article and a bound field for the description. Here is the code:

<body>
 <form id="form1" runat="server">
   <asp:GridView ID="GridView1" runat="server" 
           AutoGenerateColumns="False">
     <Columns>
       <asp:TemplateField HeaderText="Title">
         <ItemTemplate>
          <a href=<%# Eval("link") %> 
            target=_blank><%# Eval("title") %></a>
         </ItemTemplate>
       </asp:TemplateField>
       <asp:BoundField DataField="description" 
           HeaderText="Description" />
     </Columns>
   </asp:GridView>
 </form>
</body>

Now, you can copy the code for the GridView as many times you need it. Maybe a GridView for aspalliance as well.

The code behind

Import the namespace System.Data and create the Page_Load event. Then, call the function BindGrid for every RSS-feed you will display on your web page. The function BindGrid needs three parameters:

  • the string pointing to the URL of the news feed
  • the GridView object that will display the news
  • and an integer indicating which part of the feed will be bound
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles Me.Load
        BindGrid("http://www.codeproject.com/" & _ 
                 "webservices/articlerss.aspx", _
                 GridView1, 3)
        BindGrid("http://aspalliance.com/rss.aspx", _
                 GridView2, 2)
    End Sub
    Sub BindGrid(ByVal strRss As String, ByVal oGrid _
                 As GridView, ByVal iTable As Integer)
        Dim oDs As New DataSet
        oDs.ReadXml(strRss)
        oGrid.DataSource = oDs.Tables(iTable)
        oGrid.DataBind()
    End Sub
End Class

BindGrid creates a new DataSet object and reads the feed using ReadXml. Then the XML document is bound to the GridView. You can also find an online example here: How to bind RSS feeds to a GridView.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

fstrahberger


Florian works as consultant for change- and configuration management for about 7 years. In this environment he is often forced to work with unix, perl and shell scripts.

For more information about change- and configuration management (espacially Serena Dimensions) visit: www.venco.de

For video tutorials about asp.net, ajax, gridviews, ... (in german) visit: www.siore.com
Occupation: Web Developer
Location: Germany Germany

Other popular ASP.NET Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAutoGenerateColumnsmemberMBrooker12:02 30 Nov '06  
GeneralNice, concrete articlememberAdrian Murillo9:23 15 Nov '06  
GeneralRe: Nice, concrete articlememberfstrahberger22:07 15 Nov '06  
Generalgood work.memberMinhajkk0:34 8 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Nov 2006
Editor: Chris Maunder
Copyright 2006 by fstrahberger
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project