Skip to main content Skip to footer

Serializing Lists to IsolatedStorage - Quick and Painless

Whether it's user settings or some quick data that I want to persist between runs of my application, I've often found a need for quickly saving and loading data from IsolatedStorage. Recently, I wrote an application that needed to store a list of users. On each run of the application the users are loaded, and on close of the application the users get saved. I wrote a simple pair of methods that serialized and deserialized my list of users from IsolatedStorage using the XmlSerializer class. The XmlSerializer is great for quick and painless serialization of data lists to XML documents. With it you can serialize (or deserialize) in basically two lines of code. The following Save and Load methods use the XmlSerializer on a specific file in IsolatedStorage.


private static string _filename = "Users.xml";  

public static List<User> Load()  
{  
    List<User> users = new List<User>();  
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())  
    {  
        if (store.FileExists(_filename))  
        {  
            using (IsolatedStorageFileStream stream = store.OpenFile(_filename, FileMode.Open, FileAccess.Read))  
            {  
                using (TextReader reader = new StreamReader(stream))  
                {  
                    XmlSerializer deserializer = new XmlSerializer(typeof(List<User>));  
                    users = (List<User>)deserializer.Deserialize(reader);  
                }  
            }  
        }  
    }  
    return users;  
}  

public static void Save(List<User> users)  
{  
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())  
    {  
        using (IsolatedStorageFileStream stream = store.OpenFile(_filename, FileMode.OpenOrCreate, FileAccess.Write))  
        {  
            using (TextWriter writer = new StreamWriter(stream))  
            {  
                XmlSerializer serializer = new XmlSerializer(typeof(List<User>));  
                serializer.Serialize(writer, users);  
            }  
        }  

    }  
}  

The key lines of code for serialization are #31/32 and for deserialization are #14/15. The rest is standard IsolatedStorage I/O. User is my business entity and the beautiful thing is that it doesn't really matter what this entity looks like. It contains the expected properties like FirstName, LastName and so on, but with the XmlSerializer approach I don't have to worry about casting each individual property from XML. Using the same approach I also added the ability to save and load users from a file on disk. This uses SaveFileDialog/OpenFileDialog rather than IsolatedStorage.


public static void SaveXML(List<User> users)  
{  
    SaveFileDialog sfd = new SaveFileDialog();  
    sfd.DefaultExt = ".xml";  
    sfd.Filter = "Xml|*.xml|All Files|*.*";  

    bool? dialogResult = sfd.ShowDialog();  

    if (dialogResult == true)  
    {  
        using (Stream stream = sfd.OpenFile())  
        {  
            StreamWriter sw = new StreamWriter(stream, System.Text.Encoding.UTF8);  
            XmlSerializer serializer = new XmlSerializer(typeof(List<User>));  
            serializer.Serialize(sw, users);  
            stream.Close();  
        }  
    }  
}  

public static List<User> LoadXML()  
{  
    List<User> users = new List<User>();  

    OpenFileDialog ofd = new OpenFileDialog();  
    ofd.Filter = "Xml|*.xml";  

    bool? dialogResult = ofd.ShowDialog();  

    if (dialogResult == true)  
    {  
        using (Stream stream = ofd.File.OpenRead())  
        {  
            StreamReader sr = new StreamReader(stream, false);  
            XmlSerializer deserializer = new XmlSerializer(typeof(List<User>));  
            users = (List<User>)deserializer.Deserialize(sr);  
            stream.Close();  
        }  
    }  
    return users;  
}  

XmlSerializer saved me a lot of development time and I hope to use it again in the future.

ComponentOne Product Manager Greg Lutz

Greg Lutz

comments powered by Disqus