Save ObservableCollection to Application Storage in Windows Store APP

This article explains how to store an ObservableCollection to local storage.

Let's assume we have one Employee Class as in the following:
  1. public class Employee  
  2.     {  
  3.         public string Name { getset; }  
  4.         public string Address { getset; }          
  5.         public string Designation { getset; }   
  6.     } 
Suppose we want to store the ObservableCollection<Employee> to the local storage of Windows Store apps.

To store the collection to local storage we need to serialize the object, so for that we will create an ObjectSerializer class like the following. We will use this class to serialize the ObservableCollection to XML and vice versa.
  1. internal static class ObjectSerializer<T>  
  2.     {  
  3.         // Serialize to xml  
  4.         public static string ToXml(T value)  
  5.         {  
  6.             XmlSerializer serializer = new XmlSerializer(typeof(T));  
  7.             StringBuilder stringBuilder = new StringBuilder();  
  8.             XmlWriterSettings settings = new XmlWriterSettings()  
  9.             {  
  10.                 Indent = true,  
  11.                 OmitXmlDeclaration = true,  
  12.             };  
  13.   
  14.             using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings))  
  15.             {  
  16.                 serializer.Serialize(xmlWriter, value);  
  17.             }  
  18.             return stringBuilder.ToString();  
  19.         }  
  20.   
  21.         // Deserialize from xml  
  22.         public static T FromXml(string xml)  
  23.         {  
  24.             XmlSerializer serializer = new XmlSerializer(typeof(T));  
  25.             T value;  
  26.             using (StringReader stringReader = new StringReader(xml))  
  27.             {  
  28.                 object deserialized = serializer.Deserialize(stringReader);  
  29.                 value = (T)deserialized;  
  30.             }  
  31.   
  32.             return value;  
  33.         }  
  34.     } 
Let's say we have ObservableCollection<Employee> to save this to a local storage. We will create the StorageFile object.
  1. string rootFrameDataString = ObjectSerializer<ObservableCollection<Employee>>.ToXml(rootFrameData);  
  2.                 if (!string.IsNullOrEmpty(rootFrameDataString))  
  3.                 {  
  4.                     StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("localFileData.txt",   
  5. CreationCollisionOption.ReplaceExisting);  
  6.                     await FileIO.WriteTextAsync(localFile, rootFrameDataString);   
  7.                 }  
In the code above rootFrameData is the ObservableCollection of Employee. We have also created the StorageFile object to store the data. In the code above we have created one file named localFileData.txt.

We will write our serialized data to this file and store it to application storage. In this way we can store the ObservaleCollection to application local storage.

Now we will see how to retrieve the local storage data.
  1. StorageFile localFile;  
  2.             try  
  3.             {  
  4.                 localFile = await ApplicationData.Current.LocalFolder.GetFileAsync("localFileData.txt.");  
  5.             }  
  6.             catch (FileNotFoundException ex)  
  7.             {  
  8.                 NotifyUser(ex.Message);  
  9.                 localFile = null;  
  10.             }  
  11.             if (localFile != null)  
  12.             {  
  13.                 string localData = await FileIO.ReadTextAsync(localFile);  
  14.   
  15.                 itemCollection = ObjectSerializer<ObservableCollection<Employee>>.FromXml(localData);  
  16.             } 
In the code above we are just reading data from the file, that is stored in the local storage and deserialzing it to an ObservableCollection using ObjectSerializer.


Similar Articles