What is the best way to save a List <Object> in a Windows 8 application

I have a List<class> data. And I want to save it and retrieve it every time the application starts and exits accordingly. Which is equivalent to IsolatedStorage (WP7) in Windows 8. How do I save these settings?

+8
windows-8
source share
2 answers

On Windows 8, you must use LocalFolder for your application, which you can access using:

 StorageFolder folder = ApplicationData.Current.LocalFolder; 

and then save the help files using:

 var fileToGet = await folder.GetFileAsync("nameOfFile.fileType"); 

I am currently working in a similar situation in a project that I am working on, where I want to save a list of user objects in my LocalFolder application and reload it later.

My solution was to serialize the list into an XML string and save it in the application folder. You should be able to adapt my methods:

 static public string SerializeListToXml(List<CustomObject> List) { try { XmlSerializer xmlIzer = new XmlSerializer(typeof(List<CustomObject>)); var writer = new StringWriter(); xmlIzer.Serialize(writer, List); System.Diagnostics.Debug.WriteLine(writer.ToString()); return writer.ToString(); } catch (Exception exc) { System.Diagnostics.Debug.WriteLine(exc); return String.Empty; } 

Now that you have the line, you can save the text file and put it in LocalStorage:

 //assuming you already have a list with data called myList await Windows.Storage.FileIO.WriteTextAsync("xmlFile.txt", SerializeListToXml(myList)); 

Now, when you download your application again, you can use the download method mentioned above to get the xmlFile from LocalStorage and then deserialize it to return your list.

 string listAsXml = await Windows.Storage.FileIO.ReadTextAsync(xmlFile.txt); List<CustomObject> deserializedList = DeserializeXmlToList(listAsXml); 

Again, adapt this to your needs:

 public static List<CustomObject> DeserializeXmlToList(string listAsXml) { try { XmlSerializer xmlIzer = new XmlSerializer(typeof(List<CustomObject>)); XmlReader xmlRead = XmlReader.Create(listAsXml); List<CustomObject> myList = new List<CustomObject>(); myList = (xmlIzer.Deserialize(xmlRead)) as List<CustomObject>; return myList; } catch (Exception exc) { System.Diagnostics.Debug.WriteLine(exc); List<CustomObject> emptyList = new List<CustomObject>(); return emptyList; } } 
+8
source share

You can use this class to store and load settings:

 public static class ApplicationSettings { public static void SetSetting<T>(string key, T value, bool roaming = true) { var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings; settings.Values[key] = value; } public static T GetSetting<T>(string key, bool roaming = true) { return GetSetting(key, default(T), roaming); } public static T GetSetting<T>(string key, T defaultValue, bool roaming = true) { var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings; return settings.Values.ContainsKey(key) && settings.Values[key] is T ? (T)settings.Values[key] : defaultValue; } public static bool HasSetting<T>(string key, bool roaming = true) { var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings; return settings.Values.ContainsKey(key) && settings.Values[key] is T; } public static bool RemoveSetting(string key, bool roaming = true) { var settings = roaming ? ApplicationData.Current.RoamingSettings : ApplicationData.Current.LocalSettings; if (settings.Values.ContainsKey(key)) return settings.Values.Remove(key); return false; } } 

But you can save and load only primitive types (bool, int, string, etc.). This is why you should serialize your list in XML format or another format that can be stored in a string. You can use the following methods to serialize and deserialize an object in XML and from it:

 public static string Serialize(object obj) { using (var sw = new StringWriter()) { var serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(sw, obj); return sw.ToString(); } } public static T Deserialize<T>(string xml) { using (var sw = new StringReader(xml)) { var serializer = new XmlSerializer(typeof(T)); return (T)serializer.Deserialize(sw); } } 

See also Is there a way to store instances of custom classes in ApplicationSettings of a Windows Store application?

+5
source share

All Articles