问题
I have this method:
private IEnumerable<XElement> ReadTransactions(string file_name)
{
using (var reader = XmlReader.Create(file_name + ".xml"))
{
while (reader.ReadToFollowing("transaction", "urn:namepsaceUri"))
{
using (var subtree = reader.ReadSubtree())
{
yield return XElement.Load(subtree);
}
}
}
}
This method reads from an XML file. However, I don't need all of the nodes in the XML file at same time.
I want to get them ten at a time.
I tried working with XPathSelectElements, but that gets all the nodes, and then I need to iterate through them.
So, is there a way to get the nodes from the XML file which are 40-50? I want to modify ReadTransactions
- to have another input parameter (40 in this case), and instead of all the elements, it will return just 10?
回答1:
What about Skip() and Take() extensions methods?
var items = ReadTransactions(file_name).Skip(40).Take(10);
回答2:
what about ElementAt
seems to me this is what you are looking for
来源:https://stackoverflow.com/questions/16295086/accessing-a-range-of-elements-from-any-point-in-an-ienumerable