Search code examples
c#asp.netlistviewpaginationrss

Paging of RSS using System.ServiceModel.Syndication


I'm trying to achieve pagination of the posts i get from a RSS-feed using the System.ServiceModel.Syndication. However I cant figure out how to do this and what the best way to do it is.

As of now i use a Listview to present the data that is fetched by this in my code-behind:

// Link to the RSS-feed.
string rssUri = "feed.xml";
var doc = System.Xml.Linq.XDocument.Load(rssUri);

// Using LINQ to loop out all posts containing the information i want.
var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")

     select new
     {
          Title = el.Element("title").Value,
          PubDate = el.Element("pubDate").Value,

          Enclosure = el.Element("enclosure").Attribute("url").Value,
          Description = el.Element("description").Value
      };

// Binding the data to my listview, so I can present the data.
lvFeed.DataSource = rssFeed;
lvFeed.DataBind();

So where do I go from here? I'm guessing one way is to work with a DataPager in my Listview? However I'm uncertain how to work with that control, should I send all of my data into some list or something like IEnumerable<>?


Solution

  • After some trial and error and reading up on the DataPager i came up with the following solution that is now working very well!

    First off i created a class for my object, working with that i set up a select-method for my ListView initiating at page-load binding the data to it. And the trick here was to use the ICollection interface, and send the data to a list. This is the now working code for that select-method, hope it can help someone else out facing the same problem! :)

    ICollection<Podcast> SampleData()
    {
        string rssUri = "http://test.test.com/rss";
        var doc = System.Xml.Linq.XDocument.Load(rssUri);
    
        ICollection<Podcast> p = (from el in doc.Elements("rss").Elements("channel").Elements("item")
    
                             select new Podcast
                             {
                                 Title = el.Element("title").Value,
                                 PubDate = el.Element("pubDate").Value,
                                 Enclosure = el.Element("enclosure").Attribute("url").Value,
                                 Description = el.Element("description").Value
                                  }).ToList();
            return p;
        }