Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RyanAchtenSoma · Aug 19, 2015 at 08:54 AM · javascriptruntimesavexml

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);
         }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving gameobjects' properties for Dynamically growing prefabs 1 Answer

Saving gameobject when switching scenes in javascript 2 Answers

Questions about XML save/load 0 Answers

I cant get my Xml save script to work. I dont know how. 2 Answers

How to instantiate a Prefab ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges