You can apply several indexing operations with sequential [...] :
var doc = XDocument.Parse(xml); //the xml from your question var node = doc.XPathSelectElement("/profiles/profile[name='john'][last()]"); Console.WriteLine(node.Attribute("id").Value); //outputs 8405
This will return the last profile element containing the name element with the value john .
If on the other hand you want to return all elements for which the last name element has the value john , your XPath should work already:
var nodes = doc.XPathSelectElements("/profiles/profile[name[last()]='john']"); foreach (var node in nodes) { Console.WriteLine(node.Attribute("id").Value); }
source share