Note that there are some explanatory texts on larger screens.

plurals
  1. POWP7 IsolatedStorage XML file empty after app update
    text
    copied!<p>I submitted an update for my WP7 app and I've had users complain that their data was erased after the update. I am storing my data by serializing my ViewModel class as an XML file in IsolatedStorage. During my testing I noticed that updating the app caused the settings file to get partially overwritten with a serialized copy of a new instance (with default values) of my ViewModel object. I thought I had solved the problem by using FileMode.Create when I write to my XML file, but I guess not. </p> <p>Could the serialization have gone wrong because I added new properties to the ViewModel object and deserializing from the existing XML file failed? I do have my code set to instantiate a new ViewModel object if one cannot be read from the XML file. If this is the case, does it mean I cannot add any new properties to my ViewModel object?</p> <p>Edit:<br> Here is the structure of my ViewModel, not really all that complex: </p> <pre><code>public class MyClass: INotifyPropertyChanged { public MyClass() { // Set defaults this.Items= new ObservableCollection&lt;Item&gt;(); this.TextTemplate = "default"; this.HasSeenSomething = false; } public ObservableCollection&lt;Item&gt; Items { get; set; } // New properties added in app update public string TextTemplate { get; set; } public bool HasSeenSomething { get; set; } } </code></pre> <p>Here is the code I'm using to serialize/deserialize my ViewModel. I think it is pretty standard but maybe I've botched something: </p> <pre><code>public static void WriteToXml&lt;T&gt;(T data, string path) { var xmlWriterSettings = new XmlWriterSettings { Indent = true }; using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = myIsolatedStorage.OpenFile(path, FileMode.Create)) { var serializer = new XmlSerializer(typeof(T)); using (var xmlWriter = XmlWriter.Create(stream, xmlWriterSettings)) { serializer.Serialize(xmlWriter, data); } } } } public static T ReadFromXml&lt;T&gt;(string path) { T data = default(T); try { using (var myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (myIsolatedStorage.FileExists(path)) { using (var stream = myIsolatedStorage.OpenFile(path, FileMode.Open)) { try { var serializer = new XmlSerializer(typeof(T)); object instance = serializer.Deserialize(stream); if (instance != null) data = (T)instance; } catch (System.Exception ex) { var e = ex; } } } } } catch(System.Exception ex) { var e = ex; } return data; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload