How can I store object of a class in WP7 Isolated Storage? I want to retrieve it and edit it whenever required.
You need to serialize your data to save it, then deserialize it to load it. You can find a complete worked example in this article .
For example, mark the class and properties as follows:
[DataContract]
public class Employee
{
[DataMember]
public int EmployeeNumber { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Department { get; set; }
}
Construct a serializer:
DataContractSerializer mySerializer = new DataContractSerializer(typeof(Employee));
Then load / save via ReadObject
/ WriteObject
.
U can use xml serialisation
public static void Serialize<T>(T obj, string fileName)
{
try
{
var store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = store.OpenFile(fileName, FileMode.Create);
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, writerSettings))
{
serializer.Serialize(xmlWriter, obj);
}
stream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
public static T DeSerialize<T>(string fileName)
{
try
{
var store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = store.OpenFile(fileName, FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(stream);
}
catch (Exception ex)
{
throw ex;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.