Paging of RSS using System.ServiceModel.Syndication

瘦欲@ 提交于 2019-12-25 09:20:28

问题


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<>?


回答1:


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;
    }


来源:https://stackoverflow.com/questions/23002866/paging-of-rss-using-system-servicemodel-syndication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!