【发布时间】: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”又是如何结束的呢?