【问题标题】:How do I save an object list when I close the program, and load it when I open it again? [duplicate]如何在关闭程序时保存对象列表,并在再次打开时加载它? [复制]
【发布时间】:2015-07-25 06:50:37
【问题描述】:

如何在关闭程序时将对象列表以 XML 格式保存到计算机,并在再次打开程序时加载?

这是我要保存的对象列表的测试代码,然后在我再次打开程序时加载:

public class HighScore
{
    public string name;
    public int points;

    public HighScore(string N, int P)
    {
        this.name = N;
        this.points = P;
    }
{


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public static string name;
    public static int points;
    public static List<HighScore> scorem = new List<HighScore>();

    private void Form1_Load(object sender, EventArgs e)
    {
        scorem.Add(new HighScore("Paul", 20));
        scorem.Add(new HighScore("Robert", 30));
        scorem.Add(new HighScore("John", 35));
        scorem.Add(new HighScore("Steph", 25));
        scorem.Add(new HighScore("Seth", 40));
        scorem.Add(new HighScore("Johnny", 55));
        scorem.Add(new HighScore("Michael", 200));
        scorem.Add(new HighScore("Robertinoe", 300));
        scorem.Add(new HighScore("Marstrand", 2500));
        scorem.Add(new HighScore("Doe", 3000));

        scorem = scorem.OrderByDescending(x => x.points).ToList();

        foreach(HighScore per in scorem)
        {
           label1.Text += per.name + "  " + per.points + "\n";
        }
    }

【问题讨论】:

  • 您能否提及您已经尝试过的内容,或者更具体地说明您要问的内容(例如,您是否要将二进制数据保存到文件、JSON、XML 等)?
  • @ErikGillespie,XML 序列化似乎是我正在寻找的。​​span>
  • public class HighScore的结论是“{”。不应该是“}”。我不认为它会按所述编译。而“public partial class Form1 : Form”又是如何结束的呢?

标签: c# list object save


【解决方案1】:

一种易于实现的方法是使用binaryformatter 进行序列化。

[Serializable] 属性添加到您的高分课程:

[Serializable]
public class HighScore
{
    public string name;
    public int points;

    public HighScore(string N, int P)
    {
       this.name = N;
       this.points = P;
    }

为了保存你的数据,你序列化 List(它也有 Serializable 属性)

BinaryFormatter.Serialize(somestream,somehighsscorelist)

然后再拿回来:

List<HighScore> savedscores = (List<HighScore>)BinaryFormatter.Deserialize(somestream)

此外,我会将文件名存储为应用程序设置,并记住,如果您更改 HighScore 类的结构,您将无法反序列化您的旧文件。 XML 序列化的版本容错性更高。

【讨论】:

  • 感谢您指出 XML 序列化。我将研究 XML 序列化并尝试在我的测试程序中实现它。
【解决方案2】:

这是一个完整的例子:

[Serializable]
public class HighScore {
    public string name;
    public int points;

    public HighScore(string N, int P) {
        this.name = N;
        this.points = P;
    }

}

[Serializable]
public class GameData {
    public List<HighScore> ScoreList { get; set; }
    public GameData() {
        ScoreList = new List<HighScore>();
    }
}


    private GameData gameData = new GameData();

    private void load_Click(object sender, RoutedEventArgs e) {
        Stream stream = null;
        try {
            stream = File.Open("file.bin", FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();
            gameData = (GameData)bformatter.Deserialize(stream);
        } finally {
            if (stream != null)
                stream.Close();
        }
    }

    private void save_Click(object sender, RoutedEventArgs e) {
        Stream stream = null;
        try {
            stream = File.Open("file.bin", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();
            bformatter.Serialize(stream, gameData);
            stream.Close();
        } finally {
            if (stream != null)
                stream.Close();
        }
    }

    private void display_Click(object sender, RoutedEventArgs e) {
        foreach (HighScore per in gameData.ScoreList) {
            Console.WriteLine(per.name + "  " + per.points + "\n");
        }
    }

    private void addscore_Click(object sender, RoutedEventArgs e) {
        gameData.ScoreList.Add(new HighScore("Doe", 3000));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    相关资源
    最近更新 更多