- Home /
"Root Element Missing: XML Exception" when trying to load data from XML file
Hey Unity Forums! I am trying to create a saving system for my test game in Unity, but I keep on getting this error whenever I try and load the data from the XML file:
XmlException: Root element is missing.
System.Xml.XmlTextReaderImpl.Throw (System.Exception e) (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo (System.String res) (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.XmlTextReaderImpl.ParseDocumentContent () (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.XmlTextReaderImpl.Read () (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.XmlTextReader.Read () (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.XmlReader.MoveToContent () (at <f9ec74c9799148aaa919695e2037e193>:0)
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderGameData.Read3_GameData () (at <41673fd343b24352b82942feb35ab8c7>:0)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <1f0c1ef1ad524c38bbc5536809c46b48>:0)
Rethrow as InvalidOperationException: There is an error in XML document (0, 0).
System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader, System.String encodingStyle, System.Xml.Serialization.XmlDeserializationEvents events) (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader, System.String encodingStyle) (at <f9ec74c9799148aaa919695e2037e193>:0)
System.Xml.Serialization.XmlSerializer.Deserialize (System.IO.Stream stream) (at <f9ec74c9799148aaa919695e2037e193>:0)
DataManagement.LoadData () (at Assets/Scripts/Persistence/DataManagement.cs:60)
LoadGameMenu+<>c__DisplayClass2_0.<Start>b__0 () (at Assets/Scripts/UI/Main Menu/LoadGameMenu.cs:29)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
This is the DataManagement Class:
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
public class DataManagement : MonoBehaviour
{
public static DataManagement dataManagement;
public static string saveName;
private string savesDirectory;
private void Awake()
{
if (dataManagement == null)
{
DontDestroyOnLoad(gameObject);
dataManagement = this;
}
else if (dataManagement != this)
{
Destroy(gameObject);
}
savesDirectory = Path.Combine(Application.persistentDataPath, "saves");
if (!Directory.Exists(savesDirectory))
Directory.CreateDirectory(savesDirectory);
}
public void SaveData(int playerLives, string currentLevel, Checkpoint lastCheckpoint)
{
var serializer = new XmlSerializer(typeof(GameData));
var data = new GameData();
data.playerLives = playerLives;
data.currentLevel = currentLevel;
data.checkpointX = lastCheckpoint.transform.position.x;
data.checkpointY = lastCheckpoint.transform.position.y;
data.checkpointZ = lastCheckpoint.transform.position.z;
using (var stream = new FileStream(Path.Combine(savesDirectory, saveName + ".xml"), FileMode.Create))
{
serializer.Serialize(stream, data);
}
}
public GameData LoadData()
{
string loadedFile = Path.Combine(savesDirectory, saveName + ".xml");
if (File.Exists(loadedFile))
{
var serializer = new XmlSerializer(typeof(GameData));
using (var stream = new FileStream(Path.Combine(savesDirectory, saveName + ".xml"), FileMode.Create))
{
return serializer.Deserialize(stream) as GameData;
}
}
throw new FileNotFoundException();
}
}
public class GameData
{
public int playerLives;
public string currentLevel;
public float checkpointX;
public float checkpointY;
public float checkpointZ;
}
And here is the Test.xml file generated by the code:
<?xml version="1.0"?>
<GameData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<playerLives>3</playerLives>
<currentLevel>Level 1</currentLevel>
<checkpointX>-6.28</checkpointX>
<checkpointY>-0.43</checkpointY>
<checkpointZ>4.046777</checkpointZ>
</GameData>
Thanks for the help!
I have the same problem..did you find any solution to that?
Answer by Priyanka-Rajwanshi · Jun 28, 2020 at 10:11 AM
Try using StringReader for deserialization.
public GameData LoadData()
{
if (File.Exists(filePath))
{
string fileText = File.ReadAllText(filePath);
XmlSerializer serializer = new XmlSerializer(typeof(GameData));
using (StringReader reader = new StringReader(fileText))
{
return (GameData)(serializer.Deserialize(reader)) as GameData;
}
}
return null;
}
See this link for more information, http://codesaying.com/parse-xml-in-unity3d/
This link may work for simple stuff like
example
but with real world problems no use I see this link hanging around the answers here like it it a bible of serialization the article there is about parsing an existing X$$anonymous$$L file most problems where I see this link are serialization related.
Your answer
Follow this Question
Related Questions
Problems with the XML Serializer 1 Answer
PlayerPrefs v. XML File for Saving Data? 1 Answer
Xml Serialization of "sub classes" 0 Answers
URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer
Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers