C#将XML值加载到以名称作为索引的字典中
我以这个XML为例C#将XML值加载到以名称作为索引的字典中,c#,xml,linq,dictionary,C#,Xml,Linq,Dictionary,我以这个XML为例 <DEV> <families> <Family Name = "King"> <Child Num = "1"> <ChildName> John </ChildName> <Details> <Height> 1.80 </Height>
<DEV>
<families>
<Family Name = "King">
<Child Num = "1">
<ChildName> John </ChildName>
<Details>
<Height> 1.80 </Height>
<Weight> 78 </Weight>
<Age> 16 </Age>
</Details>
</Child>
<Child Num = "2">
<ChildName> Jim </ChildName>
<Details>
<Height> 1.90 </Height>
<Weight> 88</Weight>
<Age> 18</Age>
</Details>
</Child>
</Family>
<Family Name = "Trud">
<Child Num = "1">
<ChildName> Bill </ChildName>
<Details>
<Height> 1.50 </Height>
<Weight> 78 </Weight>
<Age> 27 </Age>
</Details>
</Child>
<Child Num = "2">
<ChildName> Alise </ChildName>
<Details>
<Height> 1.40 </Height>
<Weight> 56</Weight>
<Age> 12</Age>
</Details>
</Child>
</Family>
</Families>
</DEV>
Dictionary weights=文档
.后代(“子女”)
.ToDictionary(
e=>e.Element(“ChildName”).Value,
e=>(双)e.元素(“细节”).元素(“重量”);
string[] arr = XDocument.Load(path + @"\Pages\Fams.xml").Descendants("Weight")
.Select(element => element.Value).ToArray();
Dictionary<string, double> weigths = document
.Descendants("Child")
.ToDictionary(
e => e.Element("ChildName").Value,
e => (double) e.Element("Details").Element("Weight") );