WWWCoder.com Resource Directory

Bind a Datalist to a Remote XML File
10/11/2003 5:31:32 PM

In this article we will cover binding a remote XML file to a datalist. In this specific example, the remote file will be an RSS file containing syndicated content.

The common method of binding data to a DataList or DataGrid is binding to SQLDataReader or SQLDataSet. In this article we will cover retrieving a remote RSS file and binding it to a datalist.

On my WWWCoder site, I accept RSS file submissions as part of accepting RSS files, I accept the feed URL from a textbox, retrieve the RSS file, and then bind this XML to the grid for display. If the gird throws and error, it is due to the XML file not being properly formatted. The following code demonstrates binding the RSS file to the datalist.

The first thing we'll do is place a datagrid in the aspx page. The ID of the list is dataRSSItems and bound to the following nodes from the XML file: "title", "description", and "link".

<asp:DataList ID="dataRSSItems" Runat="server" RepeatLayout="Table" 
   AlternatingItemStyle-BackColor=AliceBlue 
   SelectedItemStyle-BackColor=#ffff00 
   RepeatDirection="Vertical" RepeatColumns="1">
   <SelectedItemStyle BackColor="Yellow"></SelectedItemStyle>
   <ItemTemplate>
      <asp:LinkButton ID="btnTitle" CssClass="subsubhead" 
         Text='<%#Container.DataItem("title")%>' Runat="server" 
         CommandName="select">
       </asp:LinkButton>
       <br>
       <asp:Label Runat="server" ID="lblRssDescription" 
         CssClass="normal" Text='<%#Container.DataItem("description")%>'>
       </asp:Label>
       <br>
       <asp:HyperLink target="window" ID="lnkSiteURL" 
         CssClass="normal" Runat="server" 
         NavigateUrl='<%#Container.DataItem("link")%>' 
         text='<%#Container.DataItem("link")%>'>
       </asp:HyperLink> 
    </ItemTemplate> 
</asp:DataList>

You can see from the code above that we have to refer to our XML nodes by name within the RSS file. Then in the code behind we'll make a request for a remote RSS file and bind this XML feed to our datalist. The first function receives some string containing the value of the RSS file URL to retrive from teh Internet. You can see the function GetXMLContent will return an XMLReader. This XMLReader will then be bound to the datalist using the BindRSSFeeds method.

Private Function GetXMLContent(ByVal ContentURL As String) _
   As XmlReader
  Try
    'create an HTTP request.
    Dim wr As HttpWebRequest = CType(WebRequest.Create(ContentURL), _
       HttpWebRequest)    
    wr.Timeout = 10000 ' 10 seconds
    'get the response object.
    Dim resp As WebResponse = wr.GetResponse()
    Dim stream As stream = resp.GetResponseStream()
    ' load XML document
    Dim reader As XmlTextReader = New XmlTextReader(stream)
    reader.XmlResolver = Nothing
    Return reader
  Catch ex As Exception
    'return some error code.
  End Try
End Function
Private Sub BindRSSFeeds(ByVal inURL As String)
   Try
      Dim myRSSFeed As New DataSet
      myRSSFeed.ReadXml(GetXMLContent(inURL))
      'now refer to the item node, which we can then obtain
      'all our RSS news items underneath the path.
      dataRSSItems.DataSource = myRSSFeed.Tables("item")
      dataRSSItems.DataBind()
   Catch ex As Exception
     'return some error code.
   End Try
End Sub

Now that our gird is defined and we have the methods written to obtain the RSS feed and bind it to the grid, we'll place a button on our form to handle an event from the user, and a textbox control to accept a URL submission in the form.

<asp:Textbox ID="txtRSSURL" runat="Server" Columns="45">
  http://
</asp:TextBox>
<asp:LinkButton ID="cmdSubmitRSS" Runat="server" 
     CssClass="CommandButton">
   Submit RSS File
</asp:LinkButton>

Now in our code behind page we'll capture the link click and then call our BindRSSFeeds with the value of the textbox passed to the method to get the XML and bind it to the datalist.

Private Sub cmdSubmitRSS_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) _
  Handles cmdSubmitRSS.Click
  Try
    BindRSSFeeds(txtRSSURL.Text)
  Catch ex As Exception
    'throw some error.
  End Try
End Sub

Now we should end up with a datalist containing the RSS items from the remote RSS file.

By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.

Related Articles
   Related Document Adding Attributes to an Object Within a Datalist
   Related Document Binding the Contents of a Directory to a Datalist


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 10/8/2007 4:29:06 AM by Anonymous
Comments: Hello, my name is poor credi tloan. I found super site wuth:
http://payloan.t35.com/payday-loan-56.htmlhttp://payloan.t35.com/payday-loan-342.htmlhttp://payloan.t35.com/payday-loan-391.html Gratz!.
<URLsWithURL>
Left on 7/20/2007 12:07:12 AM by Anonymous
Comments: fine
Left on 11/15/2006 12:22:44 AM by Anonymous
Comments:
No ratings available.
Left on 3/7/2006 4:11:18 PM by Anonymous
Comments: excellent, this article really help me on my project
Left on 3/7/2006 1:17:37 PM by Anonymous
Comments: Changed my life for the better
Left on 10/25/2005 1:39:47 AM by Anonymous
Comments: Crap!
Left on 3/19/2005 6:20:57 PM by Anonymous
Comments: The title says DataLIST you are using a DataGRID. So could you do an example for the title you gave?
Left on 11/10/2004 5:09:59 PM by Patrick Santry
Comments: This code isn't for binding RSS 2.0, it works totally fine for .98. And I'm not trying to waste your time.
No ratings available.
Left on 8/3/2004 2:07:07 PM by Anonymous
Comments: First of all, tell me why I have to use a HTTPrequest to retrieve the XML when you can do that much easier with a XMLTextReader object. Second, this code plain doesn't work.
Left on 2/3/2004 5:14:16 PM by Anonymous
Comments: ditto - did not work for rss or any xml based file. Whats up with that? :-p
Left on 1/26/2004 7:17:39 AM by Anonymous
Comments: none of the code seemed to work no matter what I did
Left on 10/26/2003 1:23:56 AM by Anonymous
Comments: good
     

 

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use