Serializing and deserializing a list of objects in a Windows Phone 8 app

后端 未结 2 930
隐瞒了意图╮
隐瞒了意图╮ 2020-12-20 02:36

Given some list of objects:

List carlist = new List();

How can I serialize this list as an XML or binary file and des

相关标签:
2条回答
  • 2020-12-20 03:29

    I'm using these methods to Save and Load from a XML file in/to the 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;
        }
    }
    

    You can customize the error handling in the catch().

    Also, you can adjust the Load method to your needs, in my case I am trying to load from a file and if doesn't exist, it creates a default one and puts the default serialized object of the type provided according to the constructor.

    UPDATE :

    Let's say you have that list of cars :

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

    To save, you can just call them as await carlist.Save("myXML.xml"); , as it is an asynchronous Task(async).

    To load, var MyCars = await IsolatedStorageOperations.Load< List< Car> >("myXML.xml"). (I think, I haven't used it like this, as a List so far...

    0 讨论(0)
  • 2020-12-20 03:36

    DataContactJsonSerializer performs better than XmlSerializer. It creates smaller files and handles well lists inside properties.

    0 讨论(0)
提交回复
热议问题