zum*_*ard 3 c# serialization dictionary file
我有一个Dictionary对象,我想写入磁盘,并能够从磁盘读取它.理想情况下,我会避免任何第三方库.使用常规C#4有一种简单的方法吗?
答案接受了.
摘要:
选项1 - 使用JavaScriptSerializer
优点:不需要第三方图书馆.此外,使用更现代的格式,即JSON
缺点:难以阅读 - 没有形成.此外,确实需要引用不太常用的System.Web.Extension程序集,即使应用程序与Web无关.
解:
写:
File.WriteAllText("SomeFile.Txt", new JavaScriptSerializer().Serialize(dictionary));
Run Code Online (Sandbox Code Playgroud)
读:
var dictionary = new JavaScriptSerializer()
.Deserialize<Dictionary<string, string>>(File.ReadAllText("SomeFile.txt"));
Run Code Online (Sandbox Code Playgroud)
选项2 - 使用Linq到Xml
优点:不需要第三方图书馆.通常不需要添加其他引用.易于阅读.
缺点: XML不像更现代的JSON那样优先.
写:
new XElement("root", d.Select(kv => new XElement(kv.Key, kv.Value)))
.Save(filename, SaveOptions.OmitDuplicateNamespaces);
Run Code Online (Sandbox Code Playgroud)
读:
var dictionary = XElement.Parse(File.ReadAllText(filename))
.Elements()
.ToDictionary(k => k.Name.ToString(), v => v.Value.ToString());
Run Code Online (Sandbox Code Playgroud)
选项3 - 使用JSON.NET
优点:人类可读.现代格式.
缺点:必要的第三方图书馆.
解:
写:
File.WriteAllText("SomeFile.Txt", JsonConvert.SerializeObject(dictionary));
Run Code Online (Sandbox Code Playgroud)
读:
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>
(File.ReadAllText("SomeFile.txt"));
Run Code Online (Sandbox Code Playgroud)
如果没有像JSON.Net这样的第三方,请使用JavaScriptSerializer:
File.WriteAllText("SomeFile.Txt", new JavaScriptSerializer().Serialize(dictionary));
Run Code Online (Sandbox Code Playgroud)
从文件中获取字典:
var dictionary = new JavaScriptSerializer()
.Deserialize<Dictionary<string, string>>(File.ReadAllText("SomeFile.txt"));
Run Code Online (Sandbox Code Playgroud)
唯一要记住的是添加对System.Web.Extensions项目引用下的引用,然后你就可以使用JavaScriptSerializer了using System.Web.Script.Serialization;
或者使用JSON.Net, 您可以将字典序列化为JSON,然后将其写入文件,然后反序列化,如:
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("1", "Some value 1");
dictionary.Add("2", "Something");
Run Code Online (Sandbox Code Playgroud)
在文件中存储字典:
string json = JsonConvert.SerializeObject(dictionary);
File.WriteAllText("SomeFile.Txt", json);
Run Code Online (Sandbox Code Playgroud)
从文件中获取字典:
Dictionary<string, string> previousDictionary =
JsonConvert.DeserializeObject<Dictionary<string, string>>
(File.ReadAllText("SomeFile.txt"));
Run Code Online (Sandbox Code Playgroud)
有关两个选项之间的比较,请参阅:JSON.NET JsonConvert与.NET JavaScriptSerializer
编写 Dictionary 的最简单方法是创建一个列表,其中将 Dictionary 的每个条目转换为 XElement。然后创建一个根 XElement,其中列表是根的值。您想要使用 XElement 的原因是因为这样您可以使用它的Save方法将其作为 XML 存储到磁盘。在一行中执行此操作的示例(其中 d 是字典)
new XElement("root", d.Select(kv => new XElement(kv.Key, kv.Value)))
.Save(filename, SaveOptions.OmitDuplicateNamespaces);
Run Code Online (Sandbox Code Playgroud)
要将文件读入字典,请使用 XElement 的 Parse 静态方法并将文件的全部内容传递给它,该内容可以使用File.ReadAllText. Parse 返回一个 XElement 对象,即根。然后,您可以迭代根Elements()并将其转换为字典。您可以在一行中完成此操作:
var d = XElement.Parse(File.ReadAllText(filename))
.Elements()
.ToDictionary(k => k.Name.ToString(), v => v.Value.ToString());
Run Code Online (Sandbox Code Playgroud)
这是包含在方法中的上述版本:
public static void Store(IDictionary<string, string> d, string filename)
{
new XElement("root", d.Select(kv => new XElement(kv.Key, kv.Value)))
.Save(filename, SaveOptions.OmitDuplicateNamespaces);
}
public static IDictionary<string, string> Retrieve(string filename)
{
return XElement.Parse(File.ReadAllText(filename))
.Elements()
.ToDictionary(k => k.Name.ToString(), v => v.Value.ToString());
}
Run Code Online (Sandbox Code Playgroud)