ASP.Net 2.0 - Display an RSS feed on your site
If you would like to add an RSS feed from another site to yours then there are a couple of really simple ways to enabled this to happen. The example below shows how you can bind the RSS feed to a gridview control using an XMLDataSource control. First lets start with the user interface (UI) elements:
<asp:GridView
ID="gvRSS"
width="100%"
style="border: 0; margin: 0; padding: 0px"
runat="server"
AutoGenerateColumns="False"
EnableViewState="False"
gridlines="None"
allowpaging="True"
pagersettings-visible="false"
showheader="False">
<Columns>
<asp:templatefield >
<itemtemplate>
<div style="width: 95%; margin: auto; margin-bottom: 4px; border: 0;">
<span class="NewsHeader"><%#XPath("title")%></span><br />
<span class="NewsSub">Published: <%#XPath("pubDate")%></span><br />
<p class="NewsItem"><%#XPath("description")%></p>
<a href="<%#XPath("link")%>" class="RSSLink" target="_blank">[Read More...]</a>
<br /><br />
<hr class="hrNews" />
</div>
</itemtemplate>
<itemstyle horizontalalign="Left" width="100%" />
</asp:templatefield>
</Columns>
<pagersettings visible="False" />
</asp:GridView>
<asp:xmldatasource id="xmlDS" runat="server" enablecaching="true" cacheduration="60" cacheexpirationpolicy="Absolute" xpath="/rss/channel/item"></asp:xmldatasource>
<asp:label id="lblError" runat="server" visible="false" />
As you can see, we have a GridView control that has an item template where we have the elements of the RSS feed we want to show and the various CSS styles. There is also the XMLDataSource which has some built in cache options, from the example above you can see that it is currently cached for 60 minutes.
I decided to use the VB to control and bind the GridView so I could programatically adjust the settings or even use multiple feeds. Here is the VB.Net code to bind the controls:
''' <summary>
''' Load the page and set the default feed
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
BindRSSFeed()
End If
End Sub
''' <summary>
''' Bind the feed to the gridview
''' </summary>
''' <remarks></remarks>
Sub BindRSSFeed()
'In this example we set the feed URL here, however this could be DB driven or a value from the web.config
Dim strFeed As String = ""
Try
'Set the xmlDS as the feed URL specified above
xmlDSGaming.DataFile = "http://rss.thefeed.com/rss/rss.xml"
'Bind the grid with the XMLDataSource Object
gvRSS.DataSource = xmlDSGaming
gvRSS.DataBind()
lblError.Visible = False
Catch ex As Exception
'Uh oh, theres been an error so show a nice message
gvRSS.Visible = False
lblError.Visible = True
lblError.Text = "<div style=""width: 80%; margin: auto; padding: 10px 10px 10px 10px;><span class=""NormalRed"">There has been a problem with the feed, please try again later.</span></div>"
'Do something with the ex object here
' BLAH
End Try
End Sub
That's it, the feed should now bind to the grid, and with very little code! There are various
improvements
that could be made to this code but this should show a simple example of how you can add an RSS feed from another site to yours.