在C#中读写复杂数据类型

时间:2017-04-11 20:11:39

标签: c# wpf visual-studio uwp

将复杂数据类型保存和调用到本地存储文件夹的最简单方法是什么? 例如,让我们说我有一个人类:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    //other actions before returning, for e. g. informing the user that the keyboard is disabled

    return false
}

如何将Person对象(Ben)保存到本地存储文件夹。 更好的是,我如何保存列表并从该文件中读取? 我看过,最接近的答案说我应该使用SQL或只告诉我如何阅读和正确的字符串数据。

1 个答案:

答案 0 :(得分:3)

我建议您使用NewtonSoft.Json library序列化您的对象(或对象列表):

 // serialize your objects list and write the serialized string to a file:
 string serializedPersons = JsonConvert.SerializeObject(personsList);
 System.IO.File.WriteAllText(@"D:\path.txt", serializedPersons);
 .
 .
 // read your file and deserialize the json text:
 string personsFileText = System.IO.File.ReadAllText(@"D:\path.txt");
 var persons = JsonConvert.DeserializeObject<List<Person>>(personsFileText );