4

I am trying to read the RSS feed from C# code using System.ServiceModel.Syndication

var reader = XmlReader.Create(feedUrl);
var feed = SyndicationFeed.Load(reader);

Code works perfect but only gives me 25 feed items.

For the same feed url, more then hundred items are clearly visible in readers like Google reader.

How to get more then 25 feed items in SyndicationFeed ?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Nirav
  • 187
  • 3
  • 11

2 Answers2

3

In short, you can't get more than those 25 posts unless the feed provider has provided custom pagination to their feed, or perhaps by inferring a post/date structure. Just because you know there are > 25 posts doesn't mean they'll be available via the feed. RSS was designed to show the latest posts; it wasn't intended for archival needs, or intended to be used like a web service. Pagination is also not part of the RSS spec or Atom spec. See this other answer: How Do I Fetch All Old Items on an RSS Feed?

Google Reader works this way: Google's crawler detects a new feed soon after it first goes live on the internet, and the crawler keeps visiting it regularly. Each time it visits, it stores all of the new posts on Google's server. By storing the feed items as soon as its crawler finds a new feed, they have all the data going back to the start of the feed. The only way you can duplicate this functionality is to start archiving when a new feed starts, which is impractical and unlikely.

In sum, SyndicationFeed would get > 25 items if there were more than 25 items in the feed address.

Community
  • 1
  • 1
Sean Osterberg
  • 844
  • 4
  • 10
0

Try this;

private const int PostsPerFeed = 25; //Change this to whatever number you want

Then your action:

    public ActionResult Rss()
    {
        IEnumerable<SyndicationItem> posts =
            (from post in model.Posts
             where post.PostDate < DateTime.Now
             orderby post.PostDate descending
             select post).Take(PostsPerFeed).ToList().Select(x => GetSyndicationItem(x));

        SyndicationFeed feed = new SyndicationFeed("John Doh", "John Doh", new Uri("http://localhost"), posts);
        Rss20FeedFormatter formattedFeed = new Rss20FeedFormatter(feed);
        return new FeedResult(formattedFeed);
    }

    private SyndicationItem GetSyndicationItem(Post post)
    {
        return new SyndicationItem(post.Title, post.Body, new Uri("http://localhost/posts/details/" + post.PostId));
    }

In your FeedResult.cs

class FeedResult : ActionResult
{
    private SyndicationFeedFormatter formattedFeed;

    public FeedResult(SyndicationFeedFormatter formattedFeed)
    {
        this.formattedFeed = formattedFeed;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "application/rss+xml";
        using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
        {
            formattedFeed.WriteTo(writer);
        }
    }
}

Demostration is HERE. Warning though, no format for google chrome yet

Komengem
  • 3,662
  • 7
  • 33
  • 57
  • If the feed only provides 25 items, this code will not retrieve more than that, even if there are > 25 items that have historically been posted to the feed. – Sean Osterberg Mar 09 '13 at 21:19
  • Code works as per your example where I am generating my own feed, but fails when feed source is third party site. I am looking for concrete solution which can be used as generic for any RSS feed generated within my own solution or from outside world – Nirav Mar 10 '13 at 06:55
  • @Nirav try this post : http://stackoverflow.com/questions/6294948/pull-rss-feeds-from-facebook-page?rq=1 – Komengem Mar 10 '13 at 07:11