Wednesday 14 November 2007

Paging Data From an XML file

I needed to display a news feed from an xml file. My original intentions were to use XML & XSL to tranform into html but i needed paging support.

I looked at the XMLData Source control and there isno paging support built onto this control. After a look around I came across a .NET class called PagedDataSource. This class abstracts the paging functionality that is used in data grids etc.

All you have to do is load the XML into a dataset, load the pageddatasource with the data from the dataset, set the paging on, set the page index and items per page. Voila! This can now be bound to any control that supports data binding. Wicked!

Here is some sample code binding it to a DataRepeater.



  1. Dim ds As New DataSet


  2. ds.ReadXml("Data.xml")


  3. Dim pagedData As New PagedDataSource


  4. Dim dv As New DataView(ds.Tables(0))

  5. dv.Sort = "SortDate DESC"


  6. pagedData.DataSource = dv

  7. pagedData.AllowPaging = True

  8. pagedData.PageSize = ItemsPerPage


  9. pagedData.CurrentPageIndex = CurrentIndex


  10. xmlRepeater.DataSource = pagedData

  11. xmlRepeater.DataBind()


  12. btnBack.Visible = Not pagedData.IsFirstPage

  13. btnMore.Visible = Not pagedData.IsLastPage

No comments: