Introduction
In this article we will examine how to parse an RSS feed via an ASP.NET Web page using DataGrid. You can read more about RSS Feeds
Getting the Remote XML Data Into a DataGrid
In order to display the data from the ASPMessageboard.com RSS feed, the first thing we need to do is retrieve the RSS feed data (the XML content).
Code: VB.Net
Dim reader as XmlTextReader = New XmlTextReader('http://www.go4expert.com/external.php?type=rss&forumids=67');
XML content can be loaded into a DataSet using the DataSet's ReadXml() method. There are many overloaded forms of this method, one of them accepting an XmlReader as input. This means we can just pass in the XmlTextReader object that we created earlier into the DataSet's ReadXml() method.
Code: VB.Net
Dim ds as DataSet = New DataSet()
ds.ReadXml(reader)
When binding a DataSet to a DataGrid, the DataSet's first DataTable is used by default. In order to bind an alternate DataTable, rather than binding the DataSet itself to the DataGrid, we simply need to bind the appropriate DataTable. Since we are interested in the third DataTable, we can accomplish this with the following code:
Code:
DataGridID.DataSource = ds.Tables(2) DataGridID.DataBind()
Code: ASP.NET
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
recentPosts.DataSource = GetRSSFeed(<i>RSS URL</i>)
recentPosts.DataBind()
End Sub
Function GetRSSFeed(strURL as String) as DataTable
'Get the XML data
Dim reader as XmlTextReader = New XmlTextReader(strURL)
'return a new DataSet
Dim ds as DataSet = New DataSet()
ds.ReadXml(reader)
Return ds.Tables(2)
End Function
</script>
<asp:DataGrid runat="server" id="recentPosts" />
A Bit Of Styling
Code: ASP.NET
<asp:DataGrid runat="server" id="recentPosts" AutoGenerateColumns="False"
Font-Name="Arial" Font-Size="10pt"
HeaderStyle-Font-Bold="True"
HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="15pt"
AlternatingItemStyle-BackColor="#eeeeee">
<Columns>
<asp:TemplateColumn HeaderText="20 Most Recent ASP.Net Articles on Go4Expert">
<ItemTemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, "link")%>">
<%# DataBinder.Eval(Container.DataItem, "title") %>
</a> (<i><%# PrintTimeOnly(DataBinder.Eval(Container.DataItem, "datePosted")) %></i>)
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
