Posts Tagged ‘silverlight’

Serializing Objects to Isolated Storage in Silverlight 2 / Silverlight2中序列化和反序列化

July 5th, 2009

最近遇到silverlight2中序列和反序列以及存储的问题,总结了一下。

因为silverlight2库的问题,silverlight2并不支持BinaryFormatter的序列化和反序列化的方式。如果要实现序列化,可以是用XML的方式。不过我们也可以运用System.Runtime.Serialization里面的DataContractSerializer来实现。

结合silverlight2的独立储存技术,让我们来看看如何运用DataContractSerializer来serialize object. Note that the object will be serailized to an XML format and saved in the directory of “C:\users\Administrator\AppData\LocalLow\Microsoft\Silverlight\….. The default maximum volume of the XML file is 1MB which is supported by Silverlight 2. But you can apply for larger space using “TryIncreaseQuotaTo” under “IsolatedStorageFile”

So if we have an object called List<FavoriteItem> for saving user’s controls last time. We want to save it and automatically load it next time the users comes again.

You can then use the DataContractSerializer as follows to save List<FavoriteItem>data to Iso storage:

序列化List<FavoriteItem>对象

        public List<FavoriteItem> FavoriteList = new List<FavoriteItem>();
        public string strFile = "favorite.xml";

        public void saveFavorite()
        {
            try
            {
                //apply for the storage in Silverlight
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    string dirPath = "MyDir";
                    if (!store.DirectoryExists(dirPath))
                    {
                        //Create directory
                        store.CreateDirectory("MyDir");
                    }

                    string filePath = Path.Combine("MyDir", strFile);
                    //MessageBox.Show(strFile);
                    if (store.FileExists(filePath))
                    {
                        store.DeleteFile(filePath);
                    }

                    //Serialize using DateContractSerializer
                    IsolatedStorageFileStream fileStream = store.CreateFile(filePath);
                    DataContractSerializer serializer = new DataContractSerializer(typeof(List<FavoriteItem>));
                    serializer.WriteObject(fileStream, FavoriteList);
                    fileStream.Close();

                }
            }
           catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

And the following to read it:

反序列化List<FavoriteItem>

        public List<FavoriteItem> loadFavorite()
        {
            try
            {
                List<FavoriteItem> listHistory;
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    string filePath = Path.Combine("MyDir", strFile);
                    if (store.FileExists(filePath))
                    {
                        //Deserialize
                        IsolatedStorageFileStream fileStream = store.OpenFile(filePath, FileMode.Open);
                        DataContractSerializer serializer = new DataContractSerializer(typeof(List<FavoriteItem>));
                        listHistory = (List<FavoriteItem>)serializer.ReadObject(fileStream);
                        fileStream.Close();
                        return listHistory;
                    }
                    else
                    {
                        return null;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }

Thanks for visiting and pointing out mistakes. 原创转载请注明。

Alex Niu