0

I am developing an app for Windows 8 that has a localized leader board. The data for that leader board is stored in two arrays: the name and the score. (Yes, a collection probably would have worked better, but using the arrays was vastly easier in my code). I would now like to know how I can save both of these arrays and reload them.

All of the examples on saving data that I have looked at so far only show how to save settings and my 800 page C# book happened to skip saving data altogether.

My two arrays are currently declared as such:

static string[] names = new string[100];
static int[] scores = new int[100];

I am using Visual Studio Express 2012 for Windows 8 on a system running Windows 8.1 Pro.

Thank you for the help.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
user3439632
  • 55
  • 1
  • 6

2 Answers2

0

To get you started here's a bit of example code for serializing and saving in a Windows 8 app. As others have suggested, looking at the XmlObjectSerialzation stuff is useful.

string[] names = new string[100];
StorageFolder folder = ApplicationData.Current.RoamingFolder;
XmlWriterSettings set = new XmlWriterSettings();
set.Encoding = Encoding.Unicode;

XmlObjectSerializer serializer = new DataContractSerializer(typeof(string[]));
Stream stream = await folder.OpenStreamForWriteAsync("filename", CreationCollisionOption.ReplaceExisting);
XmlWriter writer = XmlWriter.Create(stream, set);
serializer.WriteObject(writer, names);
Seb
  • 61
  • 2
0

Look at the below methods in link.

What you are trying to save is just (name|score) pair. Its similar to dictionary. The below link explains it.

http://www.codeproject.com/Articles/42872/Save-Key-Value-Pairs-in-a-File

This should be fairly simple if your application is simple. But as others have mentioned , you should try serialization and xmls files if you want to expand beyond just scores or develop a complex application

Dexters
  • 2,419
  • 6
  • 37
  • 57