从C#SyndicationFeed中读取所有rss项

问题描述 投票:4回答:2

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

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

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

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

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

c# rss syndication-feed
2个回答
3
投票

简而言之,除非饲料提供者为其Feed提供自定义分页,或者可能通过推断帖子/日期结构,否则您不能获得超过这25个帖子。仅仅因为您知道有超过25个帖子并不意味着它们将通过Feed提供。 RSS旨在显示最新帖子;它不是用于存档需求,也不是用于Web服务。分页也不是RSS specAtom spec的一部分。看到这个其他答案:How Do I Fetch All Old Items on an RSS Feed?

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

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


0
投票

试试这个;

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

演示是qazxsw poi。但是警告,还没有谷歌浏览器的格式

© www.soinside.com 2019 - 2024. All rights reserved.