When I want to serialize/deserialize class in silverlight 4/5, I found XmlSerializeris not supported.
We can use class DataContractSerializer which is supported by silverlight. Below methods using DataContractSerializer to serialize/deserialize classes:
Serialize method:
Deserialize method:
We can use class DataContractSerializer which is supported by silverlight. Below methods using DataContractSerializer to serialize/deserialize classes:
Serialize method:
public string Serialize<T>(T data) { using (var memoryStream = new MemoryStream()) { var serializer = new DataContractSerializer(typeof (T)); serializer.WriteObject(memoryStream, data); memoryStream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(memoryStream); string content = reader.ReadToEnd(); return content; } } |
Deserialize method:
public T Deserialize<T>(string xml) { using( var stream = new MemoryStream(Encoding.Unicode.GetBytes(xml)) ) { var serializer = new DataContractSerializer(typeof (T)); T theObject = (T)serializer.ReadObject(stream); return theObject; } } |
No comments:
Post a Comment