【问题标题】:How to retrieve Link from Atom feed using SyndicationFeed and limiting number of items如何使用 SyndicationFeed 和限制项目数从 Atom 提要中检索链接
【发布时间】:2017-05-12 00:48:43
【问题描述】:

我对 asp.net 有点陌生,所以请多多包涵……

我正在尝试从 WordPress 站点读取和显示 Atom 提要。

在网上搜索,我能够将以下代码放在 Codebehind 中:

XmlReader reader = XmlReader.Create(myURL);
SyndicationFeed feed = SyndicationFeed.Load(reader);

foreach (var item in feed.Items)
{

    Response.Write(item.PublishDate.ToString("yyyy-MM-dd hh:mm tt"));
    Response.Write("<br/>");
    Response.Write(item.Title.Text);

}

reader.Close();

这可以很好地显示日期和时间。现在这里是我需要解决的问题:

1) 正在检索链接......

查看 MSDN 上的 SyndicationFeed 帖子,我可以看到有一个 Links 属性,但我不知道如何从提要中检索 &lt;link&gt;。有谁知道如何获得这个?

2) 限制输出数量...

现在,使用 foreach() 它会显示提要中的每一个条目。有什么想法可以限制它只显示最新的 x 数字吗?

我可以做一些类似...

while (var item in feed.Items < 5)
{

    Response.Write(item.PublishDate.ToString("yyyy-MM-dd hh:mm tt"));
    Response.Write("<br/>");
    Response.Write(item.Title.Text);

}

【问题讨论】:

    标签: c# asp.net atom-feed rss-reader syndicationfeed


    【解决方案1】:
    • 有什么想法可以限制它只显示最新 x 数字吗?
    • 正在检索SyndicationLink 的集合

    您可以(根据需要改进/空检查等):

    //Newest by date/time and take x (e.g. 5)
    foreach (var item in feed.Items.OrderByDescending(i => i.PublishDate).Take(5))
    {
         //Get the Uris from SyndicationLink
         var theLinks = item.Links.Select(l => l.Uri.ToString()).ToList();
    
         //do something with them....
         var foo = string.Join(",", theLinks);
    
        ....
    }
    

    第……

    【讨论】:

    • 完美运行!谢谢!
    猜你喜欢
    • 2011-04-23
    • 1970-01-01
    • 2013-02-06
    • 2017-11-18
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多