Here’s a simple example of selecting the last ‘N’ elements of a XML document using LINQ to XML
Consider the following XML
In order to select the last two elements, use the IEnumerable<XElement>.Reverse() which inverts the order of the elements in a sequence, and use Take(2) to return 2 elements, as shown in the following code:
static void Main(string[] args)
{
// code from DevCurry.com
XDocument xDoc = XDocument.Load("..\\..\\School.xml");
var students = xDoc.Descendants("Class").Reverse().Take(2);
foreach (var student in students)
Console.WriteLine(student.Element("Student").Value +
" " + student.Element("Name").Value);
Console.ReadLine();
}
OUTPUT
Tweet
No comments:
Post a Comment