< cars > < car name ="Toyota Coupe" > < profile name ="Vendor" value ="Toyota" /> < profile name ="Model" value ="Celica" /> < profile name ="Doors" value ="2" /> < support name ="Racing" value ="yes" /> < support name ="Towing" value ="no" /> </ car > < car name ="Honda Accord Aerodec" > < profile name ="Vendor" value ="Honda" /> < profile name ="Model" value ="Accord" /> < profile name ="Doors" value ="4" /> < support name ="Racing" value ="no" /> < support name ="Towing" value ="yes" /> </ car > </ cars >
from car in root.Elements( " car " )let profiles = from profile in car.Elements( " profile " ) select new { Name = profile.Attribute("name").Value, Value = profile.Attribute("value").Value } let supports = from support in car.Elements( " support " ) select new { Name = support.Attribute("name").Value, Value = support.Attribute("value").Value } select new Car { Name = car.Attribute("name").Value, Vendor = profiles.Single(prof => prof.Name == "Vendor").Value, Model = profiles.Single(prof => prof.Name == "Model").Value, Doors = int.Parse(profiles.Single(prof => prof.Name == "Doors").Value), RacingSupport = supports.Single(sup => sup.Name == "Racing").Value == "yes"} ;
其实更精彩的是在一个老外的blog上的一个超级查询表达方式.