隔离存储 - 如何读取附加数据

时间:2014-03-29 13:45:27

标签: c# wpf silverlight windows-phone-8 isolatedstorage

我有一个Game application(WP8),我们会保存多个数据并将其显示给用户。

我有一个带有字段noOfStonesPicked和noOfFruitsPicked的对象。

这是我的代码:

MyTopic topicObj = new MyTopic ();

for (int i = 0; i <= 2; i++)
            {
                Test mt = new Test();
                mt.noOfStonesPicked = 12;
                mt.noOfFruitsPicked= 20;
                topicObj.Stats.Add(mt);
                }                

WritetestTopicState(topicObj);

现在有3次尝试,每次尝试都有noOfStonesPicked -12 and noOfFruitsPicked - 20

现在我保存了这个:

public static void WritetestTopicState(MyTopic topic)
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (StreamWriter sw = new StreamWriter(store.OpenFile("12.xml", FileMode.Append, FileAccess.Write)))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
                        serializer.Serialize(sw, topic);
                        serializer = null;
                    }

                }
            }
            catch (Exception)
            {
                throw;
            }
        }

现在我该如何检索这些值并显示?

修改

这就是我的尝试:

public static MyTopic ReadMockTestTopicState()
        {
            MyTopic topic = null;
            try
            {
                using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // Read application settings. 
                    if (isoStore.FileExists("11.xml"))
                    {
                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            using (StreamReader SR = new StreamReader(store.OpenFile("12.xml", FileMode.Open, FileAccess.Read)))
                            {
                                XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
                                topic = (MyTopic)serializer.Deserialize(SR);
                                serializer = null;
                            }
                        }
                    }
                    else
                    {
                        // If setting does not exists return default setting.
                        topic = new MyTopic();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return topic;
        }

1 个答案:

答案 0 :(得分:1)

XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));

StreamReader reader = new StreamReader(path);
_myTopic = (MyTopic)serializer.Deserialize(reader);
reader.Close();

这应足以进行反序列化,如果MyTopic对象可正确序列化,我的意思是,如果MyTopic对象的属性正确归因于xml序列化。