Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Parsing RSS Feeds With ASP.Net (http://www.go4expert.com/articles/parsing-rss-feeds-aspnet-t4577/)

pradeep 6Jun2007 07:49

Parsing RSS Feeds With ASP.Net


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');

This line of code creates a new XmlTextReader object that reads from the XML data at the URL specified by the input parameter.Now, what we're after is having the RSS feed's data displayed in a DataGrid. We can accomplish this by loading the XML data into a DataSet, and then binding this DataSet to a DataGrid.

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()

When reading in XML, the DataSet will create a number of DataTables, one for each "level" of XML nesting. Each of these DataTable instances are accessible through the DataSet's Tables property. That is, ds.Tables(0) will get the first DataTable, which a row for each top-level element.

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:


DataGridID.DataSource = ds.Tables(2) DataGridID.DataBind()
The following code shows a complete ASP.NET Web page that will display the contents of an RSS feed in a DataGrid. Note that the code to load the RSS feed's XML content into a DataSet has been moved into a function, GetRSSFeed(), which returns the appropriate DataTable to bind to the DataGrid.


<%@ 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>)
    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()
      Return ds.Tables(2)
    End Function
  <asp:DataGrid runat="server" id="recentPosts" />

A Bit Of Styling


<asp:DataGrid runat="server" id="recentPosts" AutoGenerateColumns="False"
       Font-Name="Arial" Font-Size="10pt"
       HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
      <asp:TemplateColumn HeaderText="20 Most Recent ASP.Net Articles on Go4Expert">
          <a href="<%# DataBinder.Eval(Container.DataItem, "link")%>">
            <%# DataBinder.Eval(Container.DataItem, "title") %>
          </a> (<i><%# PrintTimeOnly(DataBinder.Eval(Container.DataItem, "datePosted")) %></i>)

clocking 12Jun2007 22:43

Do you like ASP.net?
if you agree, we'll discuss about it , ok?
hm, I program it ( I design a new website) and have mistakes.
First, it isn't compatible with SQL language.
certainly, I have to choose Access.
Do you know this mistake?

clocking 12Jun2007 22:46

Re: Parsing RSS Feeds With ASP.Net
this code is very good

pradeep 12Jun2007 23:04

Re: Parsing RSS Feeds With ASP.Net
Are you trying to say that ASP.Net does not work with MS SQL??

clocking 13Jun2007 07:08

Re: Parsing RSS Feeds With ASP.Net

Originally Posted by pradeep
Are you trying to say that ASP.Net does not work with MS SQL??

yes, and I can't do more for its

clocking 13Jun2007 07:12

Re: Parsing RSS Feeds With ASP.Net
So I have designed sql database and store procedured. Then , when I connectted to ASP, It had worked with a error item.

All times are GMT +5.5. The time now is 14:05.