将XmlDocument转换为Dictionary< string,string> [英] Converting XmlDocument to Dictionary<string, string>
本文介绍了将XmlDocument转换为Dictionary< string,string>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种使用Linq将XmlDocument
转换为Dictionary<string, string>
的好方法.
I'm looking for a good way to convert an XmlDocument
to Dictionary<string, string>
using Linq.
我的xml格式如下:
<config>
<field>value</field>
<field2>value2</field2>
</config>
我想将其放入具有如下键值对的字典中:
I'd like to put this into a Dictionary with key value pairs looking like this:
字段,值
字段2,值2
field, value
field2, value2
我想我应该使用Linq来做到这一点,但是我不确定语法.
I'm thinking I should use Linq to do this but I'm not sure about the syntax.
推荐答案
我不知道它是否是更好的方法,但它干净整洁.简单..
I dont know if its the better way, but its clean & simple..
XElement xdoc = XElement.Load("yourFilePath");
Dictionary<string, string> result = (from element in xdoc.Elements() select new KeyValuePair<string, string>(element.Name.ToString(), element.Value)).ToDictionary(x => x.Key, x => x.Value);
这篇关于将XmlDocument转换为Dictionary< string,string>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文