C#将Dictionary转换为XML


Dictionary to Element:

Dictionary<string, string> dict = new Dictionary<string,string>();
XElement el = new XElement("root",
dict.Select(kv => new XElement(kv.Key, kv.Value)));


Element to Dictionary:

XElement rootElement = XElement.Parse("<root><key>value</key></root>");
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach(var el in rootElement.Elements())
{
dict.Add(el.Name.LocalName, el.Value);
}

 

you can use ToDictionary... rootElement.Elements().ToDictionary( key => key.Name, val => val.Value);

文章来自:http://www.cnblogs.com/mapley/p/5409138.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3