-1

How do I save an object list to the computer as XML when I close the program, and load it when I open the progam again?

This is my test code with the object list I want to save, and then load when I open the program again:

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";
        }
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John
  • 41
  • 4
  • 3
    It's called [Serialization/DeSerialization.](https://msdn.microsoft.com/en-us/library/4abbf6k0%28v=vs.110%29.aspx) – Rick S May 13 '15 at 19:57
  • Could you put some mention of what you have already tried or be more specific about what you're asking (e.g. do you want to save binary data to a file, JSON, XML, etc.)? – Erik Gillespie May 13 '15 at 19:59
  • 1
    @ErikGillespie, XML serilization seems to be what I am looking for. – John May 13 '15 at 20:11
  • The conclusion of *public class HighScore* is "{". Shouldn't it be "}". I don't think it will compile as stated. And how does "public partial class Form1 : Form" end? – Peter Mortensen May 13 '15 at 21:06

2 Answers2

2

One easy-to-implement way is to serialize with the binaryformatter.

Add the [Serializable] attribute to your high score class:

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

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

To save your data, you serialize the List<T> (which also has the Serializable attriibute)

BinaryFormatter.Serialize(somestream,somehighsscorelist)

And to get it back again:

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

Also I would store the file name as an application setting and remember that if you change the structure of the HighScore class you won't be able to deserialize your old files. XML serialization would be more version tolerant.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RadioSpace
  • 953
  • 7
  • 15
  • Thank you for pointing out XML serialization. I will be looking into XML serialization and try to implement it into my test program. – John May 13 '15 at 20:10
0

This is a full example:

[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));
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tuxy42
  • 368
  • 3
  • 6