Hi Rocky,
I have spent almost 20 hours on this issue. Can you help me please?
I am trying to serialize and deserialize a class called ReturnCriteria which is inherited from CSLA.CriteriaBase.
The serialization is working fine and I have seen the xml. This is the method for serialization:
public static void SaveData<T>(string guid, T data)
{
DeleteData();
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(guid, FileMode.Create, isf))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.NewLineChars = string.Empty;
settings.Indent = false;
settings.NewLineHandling = NewLineHandling.None;
using (XmlWriter xmlwriter = XmlWriter.Create(isfs, settings))
{
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty);
XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
serializer.Serialize(xmlwriter, data, namespaces);
}
}
}
}
But for deserialization it gives me an exception:
{System.Security.VerificationException: Operation could destabilize the runtime.
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTaxReturnListControllerCriteria.Read5_Item(Boolean
isNullable, Boolean checkType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTaxReturnListControllerCriteria.Read6_Item()}
Here is the code for Deserialization:
private static T LoadData<T>(string guid)
{
T data = default(T);
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(guid, FileMode.Open, isf))
{
XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
var obj = serializer.Deserialize(isfs);
}
}
catch
{
}
}
return data;
}
Note: This code is saving and retreiving the data from IsolatedStorage of silverlight.
Hi,
Use one of the provided serializers in Csla.Serialization.Mobile namespace rather than the standard XmlSerializer.
CslaBinaryWriter - CslaBinaryReader
CslaXmlWriter - CslaXmlReader
CslaXmlBinaryWriter - CslaXmlBinaryReader
Hi Jonny,
I am using CSLA 3.8 and I can't see CSLAXmlWriter classes there. Are they in csla 3.8?
As per your suggestion, I tried this while serializing data:
var bytes = Csla.Serialization.Mobile.MobileFormatter.Serialize(CriteriaBasedata);
isfs.Write(bytes, 0, bytes.Length);
And for deserialization:
byte[] input;
using (MemoryStream ms = new MemoryStream())
{
isfs.CopyTo(ms);
input = ms.ToArray();
}
data = (T)(object)Csla.Serialization.Mobile.MobileFormatter.Deserialize(input);
While deserialization it gives me error :
No parameterless constructor defined for this object.
Should I need to have a public parameterless constructor in my criteriabase ?
Yes,
Silverlight runtime is restricted so all classes must have a public default constructor in order to be deserialized.
Thanks Jonny,
It works now after making the public parameterless ctor.
This was not the question but I wanted to raise the suggestion that you compress your data before putting into Isolated Storage. I don't know exactly what you're stuffing in isolated storage but CSLA objects when serialized can be pretty verbose - even with the adjusted serialization engine (if I remember right it does reduce it by about 80%, compression got me over 90% or less than half of what the new CSLA serialization engine did)
I still use compression with 4.5, and so I have the SharpZip zip functionality on the SL side and I just leverage that to compress and decompress from IsolatedStorage.
Copyright (c) Marimer LLC