If you are using C#, must it be xpath? For example (edited to support multiple files with the same version - post mentioned plural):
XDocument doc = XDocument.Parse(xml);
var nodes =
from file in doc.Document
.Element("GateKeeperFiles")
.Elements("File")
select new {
Node = file,
Version = new Version(
(int) file.Element("Major"),
(int) file.Element("Minor"),
(int) file.Element("Build"),
(int) file.Element("Revision"))
} into tmp
orderby tmp.Version descending
select tmp;
var mostRecentVersion = nodes.Select(x => x.Version).FirstOrDefault();
var files = nodes.TakeWhile(x => x.Version == mostRecentVersion);
foreach(var file in files) {
Console.WriteLine("{0}: {1}",
file.Version,
(string)file.Node.Element("Name"));
}
Or with 2.0 (from OP comment):
static int GetVersion(XmlNode element, string xpath) {
return int.Parse(element.SelectSingleNode(xpath).InnerText);
}
static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Version bestVersion = null;
List<XmlElement> files = new List<XmlElement>();
foreach (XmlElement file in doc.SelectNodes(
"/GateKeeperFiles/File")) {
Version version = new Version(
GetVersion(file, "Major"), GetVersion(file, "Minor"),
GetVersion(file, "Build"), GetVersion(file, "Revision"));
if (bestVersion == null || version > bestVersion) {
bestVersion = version;
files.Clear();
files.Add(file);
} else if (version == bestVersion) {
files.Add(file);
}
}
Console.WriteLine("Version: " + bestVersion);
foreach (XmlElement file in files) {
Console.WriteLine(file.SelectSingleNode("Name").InnerText);
}
}