繁体   English   中英

从C#SyndicationFeed中读取所有rss项

[英]Reading all rss items from C# SyndicationFeed

我正在尝试使用System.ServiceModel.Syndication从C#代码中读取RSS提要

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

代码工作完美,但只给我25个饲料项目。

对于相同的Feed网址,Google阅读器等读者可以清楚地看到超过一百个项目。

如何在SyndicationFeed中获得超过25个Feed项?

简而言之,除非饲料提供者为其Feed提供自定义分页,或者可能通过推断帖子/日期结构,否则您不能获得超过这25个帖子。 仅仅因为您知道有超过25个帖子并不意味着它们将通过Feed提供。 RSS旨在显示最新帖子; 它不是用于存档需求,也不是用于Web服务。 分页也不是RSS规范Atom规范的一部分 请参阅此其他答案: 如何获取RSS源上的所有旧项目?

Google阅读器以这种方式工作:Google的抓取工具在首次在互联网上发布后立即检测到新的Feed,并且抓取工具会定期访问它。 每次访问时,它都会在Google服务器上存储所有新帖子。 通过在抓取工具找到新Feed时立即存储Feed项,他们将所有数据都返回到Feed的开头。 复制此功能的唯一方法是在新的Feed启动时开始存档,这是不切实际且不太可能的。

总之,如果Feed地址中有超过25个项目, SyndicationFeed将获得> 25个项目。

试试这个;

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

然后你的行动:

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

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

演示就在这里 但是警告,还没有谷歌浏览器的格式

暂无
暂无

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

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