在Window Game中保存并加载游戏状态

时间:2012-12-27 09:46:56

标签: c# xna load storage

目前我正在使用XNA 4.0 Game Studio和Visual Studio 2010提供的Window Game Statement Management模板来开发游戏。有关于如何保存和加载游戏状态的任何建议吗?以前我开发了一个窗口手机游戏,数据存储在IsolatedStorage中。窗口游戏有什么相似之处吗?

         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForAssembly())
            {
                using(IsolatedStorageFileStream stream = file.OpenFile("class123.xml",FileMode.Create))
                {
                    XmlWriterSettings setting = new XmlWriterSettings();
                    setting.Indent = true;
                    using(XmlWriter writer = XmlWriter.Create(stream,setting))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Student));
                        serializer.Serialize(stream,new Student(){Name = "AhLim"});
                    }

                    using (XmlReader reader = XmlReader.Create(stream))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Student));
                        studentA = (Student)serializer.Deserialize(reader);
                    }
                }
            }

这是我最近在Window Game Applicaiton中使用的代码。但是,当我到达反序列化部分时,它显示InvalidOperationException未处理并且XML文档(0,0)上有错误。我可以知道问题出在哪里吗?

2 个答案:

答案 0 :(得分:3)

Windows Phone游戏中使用的代码在PC上运行正常。数据将存储在C:\ Users \\ Documents \ SavedGames

答案 1 :(得分:1)

尝试

 serializer.Serialize(writer, new Student() { Name = "AhLim" });

而不是

 serializer.Serialize(stream, new Student() { Name = "AhLim" });

看起来IsolatedStorageFileStream使用了错误的编码。