- Home /
Writing a XML file to iOS
I am trying to write to a XML on iOS, but it seems to be impossible. Tried several different this but none have worked.
I have read that iOS only have write access to it´s /Documents folder, so I have tried to set a Application.dataPath to the Documents folder. But I get this error in Xcode :
DirectoryNotFoundException: Could not find a part of the path "/Users/mariusmathisen/Library/Application Support/iPhone Simulator/7.1/Applications/6AE4AF70-EFDF-46F0-9947-5C0936F3AD27/DaXml.app/Data/Documents/gamexmldata.txt".
This is my code to write to XML :
blic void WriteToXml()
{
Debug.Log ("Du nådde metoden");
//string filepath = Application.dataPath + @"/Data/gamexmldata.xml";
XmlDocument xmlDoc = new XmlDocument();
GameControl.control.outputXml = "Du nådde WriteXML";
if(File.Exists(Application.dataPath + "/Documents/gamexmldata.txt"))
{
Debug.Log("Filen Fantes");
GameControl.control.outputXml = "DU ER I Writetoxml og filen fantes";
xmlDoc.Load(Application.dataPath + "/Documents/gamexmldata.txt");
//xmlDoc.Load(filepath);
XmlElement elmRoot = xmlDoc.DocumentElement;
// elmRoot.RemoveAll(); // remove all inside the transforms node.
XmlElement elmNew = xmlDoc.CreateElement("rotation"); // create the rotation node.
XmlElement rotation_X = xmlDoc.CreateElement("x"); // create the x node.
rotation_X.InnerText = x; // apply to the node text the values of the variable.
XmlElement rotation_Y = xmlDoc.CreateElement("y"); // create the y node.
rotation_Y.InnerText = y; // apply to the node text the values of the variable.
XmlElement rotation_Z = xmlDoc.CreateElement("z"); // create the z node.
rotation_Z.InnerText = z; // apply to the node text the values of the variable.
XmlElement navnElement = xmlDoc.CreateElement("name");
navnElement.InnerText = navn;
GameControl.control.inputXml = navnElement.InnerText;
elmNew.AppendChild(rotation_X); // make the rotation node the parent.
elmNew.AppendChild(rotation_Y); // make the rotation node the parent.
elmNew.AppendChild(rotation_Z); // make the rotation node the parent.
elmNew.AppendChild(navnElement);
elmRoot.AppendChild(elmNew); // make the transform node the parent.
xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt"); // save file.
Debug.Log("Filen er lagret");
}else{
xmlDoc.Save(Application.dataPath + "/Documents/gamexmldata.txt");
}
}
Are there any suggestion to why I can´t get the XML to write to iOS and it´s folder? What am I doing wrong?
I still get the same error :
XmlException: Document element did not appear. file:///Users/mariusmathisen/Library/Application Support/iPhone Simulator/7.1/Applications/6AE4AF70-EFDF-46F0-9947-5C0936F3AD27/Documents/gamexmldata.txt
Answer by whydoidoit · Mar 13, 2014 at 10:42 PM
If you want to use "Documents" under Application.persistentDataPath you would need to create that directory. Otherwise just use the path itself as that is where the device can write.
Also check out http://wiki.unity3d.com/index.php?title=Save_and_Load_from_XML and http://unitygems.com/xml
Your answer
Follow this Question
Related Questions
Write and Read to a iOS device! Help 1 Answer
moving to iOS- XML files 2 Answers
iPhone/iOS File URLs 1 Answer
XmlException when trying to read/write XML from iOS device 1 Answer
How to check the path on Android 0 Answers