Question

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 ?

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top