Windows phone 8 序列化和反序列化Windows Phone 8应用程序中的对象列表

Windows phone 8 序列化和反序列化Windows Phone 8应用程序中的对象列表,windows-phone-8,Windows Phone 8,给定一些对象列表: List<Car> carlist = new List<Car>(); 我使用以下方法将XML文件保存并加载到IsolatedStorage中: public static class IsolatedStorageOperations { public static async Task Save<T>(this T obj, string file) { await Task.Run(() =>

给定一些对象列表:

List<Car> carlist = new List<Car>();

我使用以下方法将XML文件保存并加载到IsolatedStorage中:

public static class IsolatedStorageOperations
{
    public static async Task Save<T>(this T obj, string file)
    {
        await Task.Run(() =>
            {
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream stream = null;

                try
                {
                    stream = storage.CreateFile(file);
                    XmlSerializer serializer = new XmlSerializer(typeof (T));
                    serializer.Serialize(stream, obj);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                }
            });
    }

    public static async Task<T> Load<T>(string file)
    {

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        T obj = Activator.CreateInstance<T>();

        if (storage.FileExists(file))
        {
            IsolatedStorageFileStream stream = null;
            try
            {
                stream = storage.OpenFile(file, FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof (T));

                obj = (T) serializer.Deserialize(stream);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
            return obj;
        }
        await obj.Save(file);
        return obj;
    }
}
公共静态类隔离存储操作
{
公共静态异步任务保存(此T obj,字符串文件)
{
等待任务。运行(()=>
{
IsolatedStorageFile存储=IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream流=null;
尝试
{
stream=storage.CreateFile(文件);
XmlSerializer serializer=新的XmlSerializer(typeof(T));
serializer.Serialize(流,obj);
}
捕获(例外)
{
}
最后
{
if(流!=null)
{
stream.Close();
stream.Dispose();
}
}
});
}
公共静态异步任务加载(字符串文件)
{
IsolatedStorageFile存储=IsolatedStorageFile.GetUserStoreForApplication();
T obj=Activator.CreateInstance();
if(storage.FileExists(file))
{
IsolatedStorageFileStream流=null;
尝试
{
stream=storage.OpenFile(文件,FileMode.Open);
XmlSerializer serializer=新的XmlSerializer(typeof(T));
obj=(T)序列化程序。反序列化(流);
}
捕获(例外)
{
}
最后
{
if(流!=null)
{
stream.Close();
stream.Dispose();
}
}
返回obj;
}
等待对象保存(文件);
返回obj;
}
}
您可以在catch()中自定义错误处理

另外,您可以根据需要调整Load方法,在我的例子中,我尝试从一个文件加载,如果不存在,它将创建一个默认的加载方法,并根据构造函数放置所提供类型的默认序列化对象

更新:

假设你有一张汽车清单:

Listcarlist=新列表()

要保存,您可以将它们称为
await carlist.save(“myXML.xml”),因为它是一个异步
任务
异步


要加载,
var MyCars=等待隔离存储操作。加载>(“myXML.xml”)。
(我想,我还没有像这样使用它,因为到目前为止,它是一个列表…

DataContactJsonSerializer比XmlSerializer性能更好。它可以创建更小的文件,并且可以很好地处理属性内部的列表。

如果它不起作用,你需要说明它是如何对你不起作用的。你期望什么?实际发生了什么?等等……你能提供这样的信息吗我是示例还是引用?@waghekapil:根据我自己的经验。您可以尝试使用List的inside属性序列化对象,XmlSerializer将跳过它。关于大小,一般来说,XML的结构比JSON大。谢谢!我通过DataContactJsonSerializer:)
public static class IsolatedStorageOperations
{
    public static async Task Save<T>(this T obj, string file)
    {
        await Task.Run(() =>
            {
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream stream = null;

                try
                {
                    stream = storage.CreateFile(file);
                    XmlSerializer serializer = new XmlSerializer(typeof (T));
                    serializer.Serialize(stream, obj);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream.Dispose();
                    }
                }
            });
    }

    public static async Task<T> Load<T>(string file)
    {

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        T obj = Activator.CreateInstance<T>();

        if (storage.FileExists(file))
        {
            IsolatedStorageFileStream stream = null;
            try
            {
                stream = storage.OpenFile(file, FileMode.Open);
                XmlSerializer serializer = new XmlSerializer(typeof (T));

                obj = (T) serializer.Deserialize(stream);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
            return obj;
        }
        await obj.Save(file);
        return obj;
    }
}