tags:

views:

14

answers:

1

I am reading an rss feed and I am binding it to a repeater. I would like to select the 3 most recent posts. in my XPathExpression I use AddSort The data are sorted just fine but when I bind to the repeater the sort is lost.

That is my first issue. I am using xpath expression [position()<=3] to limit to 3 items. This, however, occurs prior to the sort. So, I would not be getting the 3 most recent records but rather the first three from the feed which are then sorted (if I get the sorting working properly with the repeater). Here is a code snippet. Any help would be appreciated.

            Dim xpn As XPathNavigator = New XPathDocument(RssURL).CreateNavigator
        _xmlnsm = XmlHelper.GetXmlNameSpaceManager(xpn)
        Dim expr As XPathExpression
        expr = xpn.Compile(String.Format("/rss/channel/item[position()<={0}]", numRecords))
        expr.AddSort("title", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Text)

        Dim iterator As XPathNodeIterator = xpn.Select(expr.Expression, _xmlnsm)
        rptNews.DataSource = iterator
        rptNews.DataBind()
A: 

I don't think my solution is ideal, but here it is:

I created a new DataTable and added a column for each SingleNode in the XML. I populated an XPathNodeIterator like I did before and then looped through it to create each new row of the DataTable. Then I applied a sort to the DefaultView of the DataTable. Then I did ANOTHER loop - this one was of the now-populated DefaultView. I used a counter in the loop. If the counter was greater than my desired number of records, I used Rows.RemoveAt. Finally, I returned the DataTable and bound it to the repeater.

I don't like it. It feels like I am doing way too much extra work here. But... it is giving the desired result.

Michael