【问题标题】:Isolated Storage - How to read the appended data隔离存储 - 如何读取附加数据
【发布时间】:2014-03-29 13:45:26
【问题描述】:

我有一个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;
        }

【问题讨论】:

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


    【解决方案1】:
    XmlSerializer serializer = new XmlSerializer(typeof(MyTopic));
    
    StreamReader reader = new StreamReader(path);
    _myTopic = (MyTopic)serializer.Deserialize(reader);
    reader.Close();
    

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

    【讨论】:

    • 请看我的编辑,我试过了,第一次运行时它会存储和检索 3 个值,下次运行时会抛出 Unexpected XML declaration 的错误。
    • 这意味着你的 MyTopic 对象有问题。可能 MyTopic 对象的属性未正确归因于 xml 序列化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多