简体   繁体   English

从C#SyndicationFeed中读取所有rss项

[英]Reading all rss items from C# SyndicationFeed

I am trying to read the RSS feed from C# code using System.ServiceModel.Syndication 我正在尝试使用System.ServiceModel.Syndication从C#代码中读取RSS提要

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

Code works perfect but only gives me 25 feed items. 代码工作完美,但只给我25个饲料项目。

For the same feed url, more then hundred items are clearly visible in readers like Google reader. 对于相同的Feed网址,Google阅读器等读者可以清楚地看到超过一百个项目。

How to get more then 25 feed items in SyndicationFeed ? 如何在SyndicationFeed中获得超过25个Feed项?

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. 简而言之,除非饲料提供者为其Feed提供自定义分页,或者可能通过推断帖子/日期结构,否则您不能获得超过这25个帖子。 Just because you know there are > 25 posts doesn't mean they'll be available via the feed. 仅仅因为您知道有超过25个帖子并不意味着它们将通过Feed提供。 RSS was designed to show the latest posts; RSS旨在显示最新帖子; it wasn't intended for archival needs, or intended to be used like a web service. 它不是用于存档需求,也不是用于Web服务。 Pagination is also not part of the RSS spec or Atom spec . 分页也不是RSS规范Atom规范的一部分 See this other answer: How Do I Fetch All Old Items on an RSS Feed? 请参阅此其他答案: 如何获取RSS源上的所有旧项目?

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. Google阅读器以这种方式工作:Google的抓取工具在首次在互联网上发布后立即检测到新的Feed,并且抓取工具会定期访问它。 Each time it visits, it stores all of the new posts on Google's server. 每次访问时,它都会在Google服务器上存储所有新帖子。 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. 通过在抓取工具找到新Feed时立即存储Feed项,他们将所有数据都返回到Feed的开头。 The only way you can duplicate this functionality is to start archiving when a new feed starts, which is impractical and unlikely. 复制此功能的唯一方法是在新的Feed启动时开始存档,这是不切实际且不太可能的。

In sum, SyndicationFeed would get > 25 items if there were more than 25 items in the feed address. 总之,如果Feed地址中有超过25个项目, SyndicationFeed将获得> 25个项目。

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 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 但是警告,还没有谷歌浏览器的格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM