日期:2014-05-18 浏览次数:21322 次
using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string xml = @"<employees> <employee1 EmpID=""1"" EmpName=""洪石丹"" EmpAge=""25"" EmpSex=""男""> </employee1> <employee1 EmpID=""6"" EmpName=""黄鑫"" EmpAge=""21"" EmpSex=""男""> </employee1> <employee1 EmpID=""2"" EmpName=""覃园"" EmpAge=""22"" EmpSex=""男""> </employee1> <employee1 EmpID=""27"" EmpName=""袁岩岩"" EmpAge=""25"" EmpSex=""男""> </employee1> <employee1 EmpID=""5"" EmpName=""江冬梅"" EmpAge=""22"" EmpSex=""男""> </employee1> <employee1 EmpID=""22"" EmpName=""瞿路"" EmpAge=""23"" EmpSex=""女""> </employee1> <employee1 EmpID=""42"" EmpName=""陈建勤"" EmpAge=""24"" EmpSex=""男""> </employee1> <employee1 EmpID=""44"" EmpName=""陈"" EmpAge=""24"" EmpSex=""男""> </employee1> <employee1 EmpID=""45"" EmpName=""aa"" EmpAge=""20"" EmpSex=""女"" /> </employees>"; var query = XElement.Parse(xml).Elements("employee1").Select(x => Convert.ToInt32(x.Attribute("EmpAge").Value)).Max(); Console.WriteLine(query); } } }
------解决方案--------------------
顺便说下,如果你查询这么写:
var query = XElement.Parse(xml).Elements("employee1").OrderByDescending(x => Convert.ToInt32(x.Attribute("EmpAge").Value)).First();