Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存的游戏数据的xml序列化_Xml_C#_Serialization_Xna_Monogame - Fatal编程技术网

保存的游戏数据的xml序列化

保存的游戏数据的xml序列化,xml,c#,serialization,xna,monogame,Xml,C#,Serialization,Xna,Monogame,我试图写一个方法来保存一些游戏的基础数据。目前,我正试图用一个父元素的格式保存数据,该父元素包含一个元素数组,其中包含索引、完成和尝试字段。这是我第一次尝试序列化中的任何内容,因此我遇到了一些问题,目前,当我尝试反序列化底部异常的文件详细信息时,它遇到了invalidoperationexception 这是代码 public void SaveData() { const string filename = "data.vision"; #if WINDOWS_PHONE I

我试图写一个方法来保存一些游戏的基础数据。目前,我正试图用一个父元素的格式保存数据,该父元素包含一个元素数组,其中包含索引、完成和尝试字段。这是我第一次尝试序列化中的任何内容,因此我遇到了一些问题,目前,当我尝试反序列化底部异常的文件详细信息时,它遇到了invalidoperationexception

这是代码

public void SaveData()
{
    const string filename = "data.vision";

#if WINDOWS_PHONE
    IsolatedStorageFile dataFile = IsolatedStorageFile.GetUserStoreForApplication();
#else
    IsolatedStorageFile dataFile = IsolatedStorageFile.GetUserStoreForDomain();
#endif
    try
    {
        // Create an isolated storage stream and initialize it as null.
        IsolatedStorageFileStream isolatedFileStream = null;

        // Open the isolated storage stream, and write the save data file.
        if (dataFile.FileExists(filename))
        {
            using (isolatedFileStream = dataFile.OpenFile(filename, FileMode.Open,  FileAccess.ReadWrite))
            {
                // Read the data from the file.
                XmlSerializer serializer = new XmlSerializer(typeof(Data));
                // Store each of the deserialized data objects in the list.
                Data savedData = (Data)serializer.Deserialize(isolatedFileStream);


                // Loop through the saved data objects.
                for(int i = 0; i < savedData.Levels.Count; i++)
                {
                    // Get the data object in question.
                    LevelData levelData = savedData.Levels[i];

                    // Check to see if the index of the data object corresponds to the active level index.
                    if (levelData.Index == mLevelIndex)
                    {
                        // Check that the attempts already saved is less.
                        if (levelData.Attempts < mLevel.AttemptCounter)
                            levelData.Attempts = mLevel.AttemptCounter;

                        // Check that the 
                        if (levelData.PercentComplete < 50)
                            levelData.PercentComplete = 50;
                    }
                }
                serializer.Serialize(isolatedFileStream, savedData);
            }  
        }
        else
        {
            // If there is no data file, create a new one.
            using (isolatedFileStream = dataFile.CreateFile(filename))
            {
                // Check the file stream has been initialized.
                if (isolatedFileStream != null)
                {
                    // Create a new data object to store the meta data for the current level.
                    Data data = new Data();
                    // Create a list to store the data already saved.
                    data.Levels = new List<LevelData>();

                    // Initialize the new data values.
                    LevelData levelData = new LevelData();
                    levelData.Index = mLevelIndex;
                    levelData.Attempts = mLevel.AttemptCounter;
                    levelData.PercentComplete = 50;

                    // Add the level data.
                    data.Levels.Add(levelData);

                    // Convert the object to XML data and put it in the stream.
                    XmlSerializer serializer = new XmlSerializer(typeof(Data));
                    // Seriaize the data.
                    serializer.Serialize(isolatedFileStream, data);
                }
            }
        }
    }
    finally
    {
        // Dispose the storage file, in order to commit changes.
        dataFile.Dispose();
    }
}
数据:

水平数据

public struct LevelData
{
    /// <summary>
    /// The index of the level.
    /// </summary>
    [XmlElement(ElementName = "Index")]
    public int Index;

    /// <summary>
    /// The number of attempts the player has made for a particular level.
    /// </summary>
    [XmlElement(ElementName = "Attempts")]
    public int Attempts;

    /// <summary>
    /// A value describing the furthest the player has ever got within the level.
    /// </summary>
    [XmlElement(ElementName = "PercentComplete")]
    public int PercentComplete;
}
例外情况详情:

{System.InvalidOperationException: There is an error in XML document (10, 10). ---> System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 10, position 10.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParsePI(BufferBuilder piInDtdStringBuilder)
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlReader.ReadEndElement()
   at System.Xml.Serialization.XmlSerializationReader.ReadEndElement()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderData.Read3_Data(Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderData.Read4_Data()
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, Object events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at Vision.GameplayScreen.SaveData()
   at Vision.GameplayScreen.HandleInput(InputState input)
   at Vision.ScreenManager.Update(GameTime gameTime)
   at Microsoft.Xna.Framework.Game.<.cctor>b__19(IUpdateable updateable, GameTime gameTime)
   at Microsoft.Xna.Framework.Game.SortingFilteringCollection`1.ForEachFilteredItem[TUserData](Action`2 action, TUserData userData)
   at Microsoft.Xna.Framework.Game.Update(GameTime gameTime)
   at Vision.Game.Update(GameTime gameTime)
   at Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime)
   at Microsoft.Xna.Framework.Game.Tick()
   at MonoGame.Framework.WindowsPhone.SurfaceUpdateHandler.Draw(Device device, DeviceContext context, RenderTargetView renderTargetView)
   at MonoGame.Framework.WindowsPhone.DrawingSurfaceUpdateHandler.DrawingSurfaceContentProvider.GetTexture(Size2F surfaceSize, DrawingSurfaceSynchronizedTexture& synchronizedTexture, RectangleF& textureSubRectangle)
   at SharpDX.Direct3D11.DrawingSurfaceContentProviderShadow.DrawingSurfaceContentProviderVtbl.GetTexture(IntPtr thisPtr, IntPtr surfaceSize, IntPtr synchronizedTexture, IntPtr textureSubRectangle)}

在读取文件后和反序列化之前执行此操作:

isolatedFileStream.Position = 0;

要序列化/反序列化的代码很好。问题一定出在您对IsolatedStorageFileStream的使用上,尤其是您正在重用同一个流进行读写。我建议将其分为两个功能—一个用于保存,一个用于加载。然后,您可以调用Load、Edit、Save,如果文件不存在,则可以调用Create和Save

另外,如前所述,将Xml发布到磁盘上会很有帮助。它似乎已被加载、保存过程损坏

我在LINQPad中测试了序列化代码,如下所示:

void Main()
{
    var xml = Save();
    Console.WriteLine(xml);
    var data = Load(xml);
    Console.WriteLine("\r\nLevel count: " + data.Levels.Count.ToString());
}

// Define other methods and classes here
public struct LevelData
{
    /// <summary>
    /// The index of the level.
    /// </summary>
    [XmlElement(ElementName = "Index")]
    public int Index;

    /// <summary>
    /// The number of attempts the player has made for a particular level.
    /// </summary>
    [XmlElement(ElementName = "Attempts")]
    public int Attempts;

    /// <summary>
    /// A value describing the furthest the player has ever got within the level.
    /// </summary>
    [XmlElement(ElementName = "PercentComplete")]
    public int PercentComplete;
}

public struct Data
{
    /// <summary>
    /// The Level data object.
    /// </summary>
    [XmlArray(ElementName = "Levels")]
    public List<LevelData> Levels;
}

public string Save()
{
    Data data = new Data();
    // Create a list to store the data already saved.
    data.Levels = new List<LevelData>();

    // Initialize the new data values.
    LevelData levelData = new LevelData();
    levelData.Index = 1;
    levelData.Attempts = 3;
    levelData.PercentComplete = 50;

    // Add the level data.
    data.Levels.Add(levelData);

    // Convert the object to XML data and put it in the stream.
    XmlSerializer serializer = new XmlSerializer(typeof(Data));
    var sb = new StringBuilder();
    using (var sr = new StringWriter(sb))
    {
        // Seriaize the data.
        serializer.Serialize(sr, data);
    }
    return sb.ToString();
}

public Data Load(string xml)
{
    // Convert the object to XML data and put it in the stream.
    XmlSerializer serializer = new XmlSerializer(typeof(Data));
    using (var sr = new StringReader(xml))
    {
        return (Data)serializer.Deserialize(sr);
    }
}

显示您得到的实际异常。请发布未修复的完整异常数据和/或数据类的定义。调试语句显示,此时位置也已为0。您可以发布已创建的XML文件吗?
void Main()
{
    var xml = Save();
    Console.WriteLine(xml);
    var data = Load(xml);
    Console.WriteLine("\r\nLevel count: " + data.Levels.Count.ToString());
}

// Define other methods and classes here
public struct LevelData
{
    /// <summary>
    /// The index of the level.
    /// </summary>
    [XmlElement(ElementName = "Index")]
    public int Index;

    /// <summary>
    /// The number of attempts the player has made for a particular level.
    /// </summary>
    [XmlElement(ElementName = "Attempts")]
    public int Attempts;

    /// <summary>
    /// A value describing the furthest the player has ever got within the level.
    /// </summary>
    [XmlElement(ElementName = "PercentComplete")]
    public int PercentComplete;
}

public struct Data
{
    /// <summary>
    /// The Level data object.
    /// </summary>
    [XmlArray(ElementName = "Levels")]
    public List<LevelData> Levels;
}

public string Save()
{
    Data data = new Data();
    // Create a list to store the data already saved.
    data.Levels = new List<LevelData>();

    // Initialize the new data values.
    LevelData levelData = new LevelData();
    levelData.Index = 1;
    levelData.Attempts = 3;
    levelData.PercentComplete = 50;

    // Add the level data.
    data.Levels.Add(levelData);

    // Convert the object to XML data and put it in the stream.
    XmlSerializer serializer = new XmlSerializer(typeof(Data));
    var sb = new StringBuilder();
    using (var sr = new StringWriter(sb))
    {
        // Seriaize the data.
        serializer.Serialize(sr, data);
    }
    return sb.ToString();
}

public Data Load(string xml)
{
    // Convert the object to XML data and put it in the stream.
    XmlSerializer serializer = new XmlSerializer(typeof(Data));
    using (var sr = new StringReader(xml))
    {
        return (Data)serializer.Deserialize(sr);
    }
}