- Home /
XmlException: unexpected end of file. Current depth
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);
}
Answer by RyanAchtenSoma · Sep 17, 2015 at 09:32 AM
I couldn't find any formal documentation as to why this particular return incurred (I assume I was trying to access/add to the incorrect node), however the correct process I found for creating new nodes using XML Document is to create a new XML Element and append it to the node you want it to be a child of (as detailed in the example below).
I hope this helps anyone else in a similar situation.
Example:
doc = new XmlDocument();
doc.Load(Application.dataPath + "/objMetaTest01.xml");
root = doc.DocumentElement;
var newObjNode = doc.CreateElement("MetaPipeObject");
var fileNameNode = doc.CreateElement("FileName");
fileNameNode.InnerText = fileName;
newObjNode.AppendChild(fileNameNode);
var contribUserNode = doc.CreateElement("ContribName");
contribUserNode.InnerText = "Add Contributor Name";
newObjNode.AppendChild(contribUserNode);
contribUsr = contribUserNode.InnerText;
var impDate = System.DateTime.Now.ToString("dd/MM/yyyy");
var contribDateNode = doc.CreateElement("ContribDate");
contribDateNode.InnerText = impDate;
newObjNode.AppendChild(contribDateNode);
contribDate = contribDateNode.InnerText;
var descriptionNode = doc.CreateElement("Description");
descriptionNode.InnerText = "No Description Added Yet";
newObjNode.AppendChild(descriptionNode);
objDescript = descriptionNode.InnerText;
var modelCreatorNode = doc.CreateElement("ModelCreator");
modelCreatorNode.InnerText = "No Model Creator Added Yet";
newObjNode.AppendChild(modelCreatorNode);
modelCreatorName = modelCreatorNode.InnerText;