LINQ大师,我有一个快速的。
说我有以下XML文档:
<?xml version="1.0" encoding="utf-8"?>
<xamDataPresenter version="12.1.20121.1010" formatVersion="1.6">
<fieldLayouts>
<fieldLayout key="ems:airportsType" >
<fields>
<field name="IKO" />
<field name="Name" />
<field name="the_geom" />
<field name="DistanceKm" />
<field name="DistanceNm" />
</fields>
<recordFilters />
</fieldLayout>
<fieldLayout key="ems:airportsType" >
<fields>
<field name="IKO"/>
<field name="na3" />
<field name="Name"/>
<field name="Type"/>
<field name="the_geom"/>
<field name="Mod_time" />
<field name="Country" />
<field name="Operator"/>
<field name="Contact"/>
<field name="DistanceKm"/>
<field name="DistanceNm"/>
</fields>
<recordFilters />
</fieldLayout>
......
</fieldLayouts>
</xamDataPresenter>
我用什么LINQ语法来获取 fieldLayout 节点,其中 field 元素最多?
答案 0 :(得分:3)
var res = XDocument.Load(filename)
.Descendants("fieldLayout")
.OrderByDescending(x => x.Descendants("field").Count())
.First();
答案 1 :(得分:0)
var fieldLayout = xDoc.Root
.Element("FieldLayout")
.Elements("fieldLayout")
.OrderByDescending(fl => fl.Element("fields")
.Elements("field")
.Count())
.First();