2013-03-09 120 views
4

我想讀從C#代碼中使用System.ServiceModel.Syndication閱讀所有的RSS項目SyndicationFeed

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

代碼工作完美,但只給了我25個提要條目。

對於相同的提要網址,在Google閱讀器等閱讀器中,可以清晰地看到超過一百個項目。

如何在SyndicationFeed中獲取25個以上的Feed項目?

回答

3

總之,除非飼料提供商已經爲其飼料提供了自定義分頁,或者可能通過推斷帖子/日期結構,否則不能獲得超過25個帖子。僅僅因爲你知道有25個帖子並不意味着他們可以通過feed獲得。 RSS旨在顯示最新的帖子;它並不打算用於檔案需求,或打算用於Web服務。分頁也不是RSS specAtom spec的一部分。看到另一個答案:How Do I Fetch All Old Items on an RSS Feed?

谷歌閱讀器的工作原理是這樣的:谷歌的抓取工具在互聯網上第一次上線後不久就會檢測到一個新的抓取工具,並且抓取工具會經常訪問它。每次訪問時,它都會將所有新帖子存儲在Google服務器上。通過在抓取工具發現新的提要時立即存儲提要項目,他們將所有數據都返回到提要的起始位置。您可以複製此功能的唯一方法是在新Feed開始時開始存檔,這是不切實際和不可能的。

總之,SyndicationFeed將得到> 25個項目,如果飼料地址中有超過25個項目。

+1

似乎沒有其他方式..所以標記答案是正確的。 – Nirav 2013-03-12 13:57:00

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

筆畫演示是HERE。儘管警告,谷歌Chrome瀏覽器沒有格式

+0

如果供稿僅提供25個項目,即使歷史記錄已將大於25個項目發佈到供稿中,此代碼也不會檢索到更多內容。 – 2013-03-09 21:19:57

+0

代碼根據您的示例生成我自己的Feed,但在Feed源是第三方站點時失敗。我正在尋找具體的解決方案,可以用作我自己的解決方案或外部世界中生成的任何RSS提要的通用方法 – Nirav 2013-03-10 06:55:06

+0

@Nirav試試這篇文章:http://stackoverflow.com/questions/6294948/pull-rss-feeds-從Facebook的頁面?RQ = 1 – Komengem 2013-03-10 07:11:24