【问题标题】:write into a text file from a list<T> c# [duplicate]从列表<T> c#写入文本文件
【发布时间】:2017-03-05 13:22:44
【问题描述】:

我正在使用 c#,并且我有一个类型列表,其中 Family 是具有这种结构的类:

public class Family
    {
        public Father father;
        public Mother mother;
        public int IdFamily { get; set; }
    }

父亲和母亲也是具有自己属性的类。

我的名单上有关于这些家庭的数据。我想将列表中的所有数据存储到 txt 文件中。

如何使用 System.IO 和 StreamWriter 做到这一点?

【问题讨论】:

  • 什么结构? XML? JSON?还有什么?
  • 格式重要吗?具体来说,为什么不直接序列化为 JSON 字符串,然后将其写入文件?
  • 格式无所谓
  • Go for JSON,Newtonsoft JSON 易于使用且性能良好
  • 您能否添加一个列表样本,您想从中提取信息?查看那里存储了哪些类型的信息

标签: c# list


【解决方案1】:

如果您选择 Json,那么使用 Newtonsoft 就很简单了

string json = JsonConvert.SerializeObject(family, Formatting.Indented);
File.WriteAllText(path, json);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-19
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多