Hi Unity Community!
I am trying to save some data to an existing .xml file instead of via binary serialisation method. I require that the majority of the nodes, elements and their text components be added at runtime. However I keep encountering this error:
XmlException: unexpected end of file. Current depth is 1 file:///Users/ryanachten/Documents/UnityTests/MetaPipeline/Assets/objMetaTest01.xml Line 5, position 15.
The file this is pointed to is located in the project’s Assets folder.
Can someone please explain to me where I am going wrong here? I’m not sure what is referred to by the “Current depth is 1 file” part of the return and can’t seem to find examples relevant to my issue.
Many thanks in advance!
Ryan
The .xml code I am trying to parse and add to is:
<?xml version="1.0" encoding="utf-8"?>
<xml id="objMetaTest01" style="display:none">
<!-- Test01 XML saved data for MetaPipe -->
<MetaObjects>
</MetaObjects>
And the JS function being called is:
public function Save(){ //this could be changed to OnDisable for autosave
//var filePath : String = "/Users/ryanachten/Documents/UnityTests/MetaPipeline/Assets/objMetaTest01.xml"; //location of XML file
var filePath : String = Application.dataPath + "/objMetaTest01.xml";
var xmlDoc = new XmlDocument(); //***HERE***
//if(File.Exists(filePath)){ //if the XML file is at the correct location
xmlDoc.Load(filePath); //load xml file
var rootNode = xmlDoc.DocumentElement; //
var modelNode = xmlDoc.CreateElement(objName); //create a new element with the object's name
var healthNode = xmlDoc.CreateElement("health");
var healthStr = health.ToString();
healthNode.InnerText = healthStr;
var expNode = xmlDoc.CreateElement("experience");
var expStr = experience.ToString();
expNode.InnerText = expStr;
var objNameNode = xmlDoc.CreateElement("objName");
objNameNode.InnerText = objName;
var fileNameNode = xmlDoc.CreateElement("fileName");
fileNameNode.InnerText = fileName;
modelNode.AppendChild(healthNode); //append children to modelNode
modelNode.AppendChild(expNode);
modelNode.AppendChild(objNameNode);
modelNode.AppendChild(fileNameNode);
rootNode.AppendChild(modelNode); //append new modelNode to root
xmlDoc.Save(filePath);
}