关闭程序时如何保存对象列表,再次打开时加载它?

时间:2015-05-13 19:54:20

标签: c# list object save

当我关闭程序时,如何以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";
        }
    }

2 个答案:

答案 0 :(得分:2)

一种易于实现的方法是使用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&lt; T&gt; (也有Seri​​alizable attriibute)

BinaryFormatter.Serialize(somestream,somehighsscorelist)

再次取回它:

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

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

答案 1 :(得分:0)

这是一个完整的例子:

[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));
    }