- Home /
Writing to an XML File?
I simply want to add a element to an already existing element in an XML file through javascript. Here is my code:
function WriteToXmlFile(){
var xmlDoc : XmlDocument = new XmlDocument();
xmlDoc.Load(Application.dataPath + "/data/saveWorld.xml");
elmRoot = xmlDoc.DocumentElement;
element = xmlDoc.CreateElement("block");
attribute = xmlDoc.CreateAttribute("ID");
elmRoot.AppendChild(element); //append "block" element to "blocks" element
xmlDoc.Save(Application.dataPath + "/data/saveWorld.xml");
}
Like I said before I create an element called "block" then I want to make it a child of the root element (which btw is called "blocks"). Therefore the whole document should look like this:
But for some reason it doesn't, instead the script makes it looks like this:
Why does this happen and how can I fix it to look like the first tree?
Thanks!
Answer by BerggreenDK · Oct 27, 2011 at 07:34 PM
I dunno if this page will help you further?
http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML
Your answer
![](https://koobas.hobune.stream/wayback/20220613023304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Write to XML-file within Assets\Resources 1 Answer
Editing an XML File 1 Answer
Write and Read to a iOS device! Help 1 Answer
Problems writing to existing xml 1 Answer
How to write a Xml file better? 3 Answers