Mar 18 2006

Top X Records with XMLDataSource

Category: ASP.NET 2.0 - GeneralBil@l @ 20:55

I had a question from a friend of mine at work, he's using the DataList with the XmlDataSource. He wanted to get the Top X records fro the XML file to show them in the DataList, so here is a simple way of doing so I found after doing some search online:


asp:XmlDataSource DataFile=Path to XML File" ID="XmlDataSource1"
server" XPath="rss/channel/item[position() < 11]">

As you can see, we are retrieving the TOP 10 Records from the XML file by using the POSITION() method to get less than 11 records, which is 10 or less records.

The DataList is a simple one:

<asp:datalist id="DataList1" runat="server" datasourceid="XmlDataSource1">
                <a href="<%# XPath("link") %>"><%# XPath("title") %></a>

We are binding the DataList to the XmlDataSource and displaying the links retrieved.

Hope this helps,



Comments are closed