1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Parsing RSS Feeds With ASP.Net

Discussion in 'ASP.NET' started by pradeep, Jun 6, 2007.

  1. pradeep

    pradeep Team Leader

    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:
     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:
    Dim ds as DataSet = New DataSet()
    ds.ReadXml(reader)
    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:

    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.

    Code:
      <%@ 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: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>  
     
  2. clocking

    clocking New Member

    nothing

    hi!
    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?
     
  3. clocking

    clocking New Member

    hi!
    this code is very good
     
  4. pradeep

    pradeep Team Leader

    Are you trying to say that ASP.Net does not work with MS SQL??
     
  5. clocking

    clocking New Member

    yes, and I can't do more for its
     
  6. clocking

    clocking New Member

    So I have designed sql database and store procedured. Then , when I connectted to ASP, It had worked with a error item.
     

Share This Page