I'm trying to serialize an ObservableCollection in a Win 8 Store App. I had already done this for a Windows Phone 8 App but I am a little lost on how to do it in Windows 8.
I know that the IsolatedStorageFile doesn't work in Windows 8. I'm using the StorageFile class to achieve it, however I'm not sure how to use it :-(
Here is the code on how I did it in a Windows Phone 8 App:
try
{
DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<NewsByTag>));
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isf.FileExists(sFileName))
isf.DeleteFile(sFileName);
using (var stream = isf.CreateFile(sFileName))
{
serializer.WriteObject(stream, data);
stream.Close();
}
}
}
... and this the one I'm trying to do in a Windows 8 Store App:
try
{
DataContractSerializer serializer = new DataContractSerializer(typeof(ObservableCollection<NewsByTag>));
StorageFile sfFavourites = await localFolder.CreateFileAsync(sFileName);
using (StorageStreamTransaction sfFav = await sfFavourites.OpenTransactedWriteAsync())
{
serializer.WriteObject(sessioinOutputStream.ToString(), data);
}
}
Also, in the WP8 I was serializing to an XML file. But, for Windows 8 I'm don't know what would be the best. XML or JSON?
Does anyone have a good example how to do it?
Any help would be very appreacite ;-)
Regards!
JsonConvert.SerializeObject(collection);
for this.JsonConverter
from Json.NET library.