得到的结果的特定数量从一个如下XPath查询 [英] Get a specific number of results from an XmlDocument XPath query

查看:252
本文介绍了得到的结果的特定数量从一个如下XPath查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查询一个Twitter的RSS源,并将结果提供到用于显示的中继器。我想只获得第5的结果XPath查询。有没有办法做到这一点在XPath语法还是我不得不遍历产生的的XmlNodeList 拉出第5?

  XmlDocument的DOC =新的XmlDocument();
XmlTextReader的读者= XmlTextReader的新(rssPath);
doc.Load(读卡器);XmlNodeList中的项目= doc.SelectNodes(/ RSS /渠道/项目);rptTwitter.ItemDataBound + =新RepeaterItemEventHandler(rptTwitter_ItemDataBound);
rptTwitter.DataSource =物品;
rptTwitter.DataBind();


解决方案

试试这个XPath查询来代替:

 (/ RSS /渠道/项)位置()< = 5]

它只返回前五个匹配的项目。括号是重要的,因为没有他们的 [位置()< = 5] 部分适用于项目元素的在其母公司,而不是其在结果节点集中位置的位置。

I'm querying a Twitter RSS feed and supplying the results into a Repeater for display. I'd like to only get the first 5 results of the XPath query. Is there a way to do that in the XPath syntax or do I have to loop over the resulting XmlNodeList to pull out the first 5?

XmlDocument doc = new XmlDocument();
XmlTextReader reader = new XmlTextReader(rssPath);
doc.Load(reader);

XmlNodeList items = doc.SelectNodes("/rss/channel/item");

rptTwitter.ItemDataBound += new RepeaterItemEventHandler(rptTwitter_ItemDataBound);
rptTwitter.DataSource = items;
rptTwitter.DataBind();

解决方案

Try this XPath query instead:

(/rss/channel/item)[position() <= 5]

It returns only the first five matching items. The parentheses are important, as without them the [position() <= 5] part applies to the item element's position in its parent rather than its position in the result node set.

这篇关于得到的结果的特定数量从一个如下XPath查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆