Get a certain amount of results from an XmlDocument XPath query

I request a Twitter RSS feed and submit the results to a repeater for display. I would like to get only the first 5 results of an XPath query. Is there a way to do this in XPath syntax, or do I need to loop on the resulting XmlNodeListone to pull 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();
+5
source share
2 answers

Try the XPath query instead:

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

It returns only the first five matching elements. The brackets are important because without them, the part [position() <= 5]is applied to the position of the element itemin its parent element, and not at its position as a result of the node.

+9

xpath, position() xpath. - ...

[position() < 6]

... 5 . Welbog - (+1 Welbog).

, .NET 3.5, ...

, < 10 ? !

... API- , RSS-. , 5 , LINQ. , .

, ..

, , API .NET. , .

, , 5 , ...

var xmlr = XmlReader.Create("http://twitter.com/statuses/public_timeline.rss")
var first5Items = SyndicationFeed
                    .Load(xmlr)
                    .GetRss20Formatter()
                    .Feed
                    .Items
                    .Take(5);  
+8

All Articles