I have the following XML
file. I want to get Max(\"NR\")
using LINQ
. Could anyone help me to do this? I know how to do this for nodes,
You treat attributes exactly the same way you would as nodes. So for example:
int maxNr = doc.Descendants("Level")
.Max(x => (int) x.Attribute("NR"));
Note that that will give you the maximum value of NR
, not the Level
element which contains that number. For that, you'd want to either use OrderByDescending(...).First()
or use MaxBy
from MoreLINQ.
XDocument xDoc = XDocument.Load(@" your XML file path ");
int maxNr = xDoc.Root.Elements().Max(x => (int)x.Element("NR"));
After you specify the file path, you can get "NR" using the Element.