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:
It returns only the first five matching items. The parentheses are important, as without them the
[position() <= 5]part applies to theitemelement’s position in its parent rather than its position in the result node set.