如何使用XML文件中的数据表和数据集存储类型为<string, string>
的字典对象?我能够保存文本字段值,但无法存储和检索字典数据。
答案 0 :(得分:1)
尝试使用linq2xml。
例如,您有字典:
var dict = new Dictionary<string, string>
{
{ "1", "aa" }, { "2", "bb" }, { "3", "cc" }
};
将其保存到xml文件:
var doc = new XElement("dict");
foreach (var pair in dict)
doc.Add(new XElement("pair",
new XElement("key", pair.Key), new XElement("value", pair.Value)
));
doc.Save("test.xml");
从xml文件加载到字典:
var xml = XElement.Load("test.xml");
dict = xml.Elements("pair")
.ToDictionary(e => e.Element("key").Value, e => e.Element("value").Value);