如何从xml文件向dictional添加数据
小精灵:
我已经宣布
Dictonary<string,string> SampleDict=new Dictonary<string,string>();
我的xml文件是
<Data>
<Element ValOne="1" ValTwo="0" />
<Element ValOne="2" ValTwo="2" />
<Element ValOne="3" ValTwo="4" />
<Element ValOne="4" ValTwo="6" />
<Element ValOne="5" ValTwo="8" />
<Element ValOne="6" ValTwo="10" />
<Element ValOne="7" ValTwo="12" />
<Element ValOne="8" ValTwo="14" />
<Element ValOne="9" ValTwo="16" />
<Element ValOne="10" ValTwo="18" />
</Data>
我需要使用linq读取“valone”和“valtwo”的值,并将其插入到上面声明的听写中
以及如何将听写的内容添加到包含两列的ListView中。
请帮我做这个
提前谢谢
最佳答案
您可以为此使用linq to xml和todictionary。
var doc = XDocument.Load("path to xml");
doc.Elements("Element").ToDictionary(
elem => elem.Attribute("ValOne").Value, //Dictionary key
elem => elem.Attribute("ValTwo").Value //Dictionary value
);
这个特殊的
ToDictionary
重载使用不同的lambda为生成的集合提取键和值。