簡體   English   中英

XML 反序列化/序列化問題

[英]Problem with XML deserialization/serialization

我正在嘗試為我的游戲實現保存和加載機制。 但是由於某種原因它不想工作。 我不斷收到相同的錯誤消息: XmlException: The data at the root level is invalid. Line 1, position 1. XmlException: The data at the root level is invalid. Line 1, position 1. .. 這是我創建序列化 xml 文件的代碼:

 public void Save()
    {
        Save newsave = new Save();
        if (Time.timeScale == 0)
        {
            int unit_id = 0;
            GameObject[] units = new GameObject[check_number_of_units()];

            GameObject[] friendlies = GameObject.FindGameObjectsWithTag("Friend");
            GameObject[] enemyosldiers = GameObject.FindGameObjectsWithTag("Enemy");
            GameObject[] allunits = enemyosldiers.Concat(friendlies).ToArray();
            foreach (GameObject g in allunits)
            {
                Debug.Log(unit_id);
                Debug.Log(check_number_of_units());
                units[unit_id] = g;
                unit_id++;
            }
            newsave.cash = id.cash;
            newsave.enemy_cash = id.enemycash;
            newsave.units = units;
            newsave.scenename = SceneManager.GetActiveScene().name;
            FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
            DataContractSerializer bf = new DataContractSerializer(newsave.GetType());
            MemoryStream streamer = new MemoryStream();

            bf.WriteObject(streamer, newsave);

            file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);

            file.Close();
        }
    }

這是 Save 類:

[System.Serializable]
[DataContract]
public class Save
{
    [DataMember]
    public int cash;
    [DataMember]
    public int enemy_cash;
    [DataMember]
    public string scenename;
    [DataMember]
    public GameObject[] units;


}

這是反序列化代碼:

public void Load()
    {
        if (File.Exists(Application.persistentDataPath + "/gamesave.save"))
        {
            Save loadedsave = new Save();
            DataContractSerializer bf = new DataContractSerializer(loadedsave.GetType());
            FileStream file = File.Open(Application.persistentDataPath + "/gamesave.save", FileMode.Open);
            loadedsave = (Save)bf.ReadObject(file);
            file.Close();
            scene_has_been_loaded = true;
            units = loadedsave.units;
            cash = loadedsave.cash;
            enemycash = loadedsave.enemy_cash;
            SceneManager.LoadScene(loadedsave.scenename);

        }
    }

我迷路了。 更令人困惑的是,錯誤僅在allunits[]不為空時出現

問題出在這一行:

file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);

您通常不想通過GetBuffer()訪問流的數據,因為它返回的底層字節數組可能大於您寫入流中的數據

要么像這樣使用stream.CopyTo方法:

streamer.Seek(0, SeekOrigin.Begin); // dont forget to seek to the start of the stream
streamer.CopyTo(file);

或者直接寫入文件,根本不使用MemoryStream

FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
DataContractSerializer bf = new DataContractSerializer(newsave.GetType());
bf.WriteObject(file , newsave);
file.Close();

您可以嘗試為您的單位創建一個模型類,然后存儲重建游戲對象所需的屬性。 我不希望 GameObject 是 XmlSerializable,因為 XmlSerializer 必須知道要實例化哪個 System.Type 以填充數組。 GameObject 是一個抽象類,因此不是不可實例化的。

[DataContract]
public class Unit
{
    [DataMember]
    public bool IsFriend { get; set; }
    [DataMember]
    public string Name { get; set; }
}

請檢查您是否像為 Save 對象那樣設置了GameObject 可序列化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM