繁体   English   中英

如何使用SyndicationFeed从Atom提要中检索链接并限制项目数

[英]How to retrieve Link from Atom feed using SyndicationFeed and limiting number of items

我对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属性,但是我不知道如何从提要中检索<link> 有人知道如何得到这个吗?

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

}
  • 有什么想法可以限制它仅显示最新的 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);

    ....
}

嗯...

暂无
暂无

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

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