I keep getting this error whenever I run my code. I am new to such operations and I can not find the cause of the problem.
XmlException: Unexpected end of file.
System.Xml.EncodingStreamWrapper.ReadBOMEncoding (System.Boolean notOutOfBand)
This code saves GameObject “map”
public void SaveMap()
{
FileStream file = File.Create(Application.persistentDataPath
+ "/" + mapSaveNameInputField.GetComponent<InputField>().text + ".dat");
SaveData data = new SaveData();
data.savedMap = map;
DataContractSerializer bf = new DataContractSerializer(data.GetType());
MemoryStream streamer = new MemoryStream();
bf.WriteObject(streamer, data);
streamer.Seek(0, SeekOrigin.Begin);
file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);
file.Close();
Debug.Log("Saved");
}
This code loads GameObject “map”
The exception shows up whenever LoadMap() is called
public void LoadMap()
{
if (File.Exists(Application.persistentDataPath
+ "/" + mapLoadNameInputField.GetComponent<InputField>().text + ".dat"))
{
FileStream file = File.Create(Application.persistentDataPath
+ "/" + mapSaveNameInputField.GetComponent<InputField>().text + ".dat");
DataContractSerializer bf = new DataContractSerializer(typeof(GameObject));
SaveData data = (SaveData)bf.ReadObject(file);
map = data.savedMap;
MemoryStream streamer = new MemoryStream();
file.Close();
Debug.Log("Loaded");
}else
{
Debug.Log("No such map");
}
}
[DataContract]
class SaveData
{
[DataMember]
private GameObject _savedMap;
public GameObject savedMap
{
get { return _savedMap; }
set { _savedMap = value; }
}
}