- Home /
XmlException when trying to read/write XML from iOS device
I am trying to read and write XML to iOS. Everything works fine in the iOS simulation, but when I try to build an app on my device the problems seems to be starting.
I have added the xml file to Copy Bundle Resources with "Copy items into destination group´s folder (if needed) check and "Create groups for any added folders" checked.
This is my code for loading the XML file :
string path = path = Application.persistentDataPath + "/gamexmldata.txt";
public void LoadFromXml()
{
Debug.Log ("Du nådde LoadXML metoden");
XmlDocument xmlDoc = new XmlDocument();
if(File.Exists(path))
{
Debug.Log("Filen finnes");
Debug.Log(path.ToString());
xmlDoc.Load(path);
XmlNodeList transformList = xmlDoc.GetElementsByTagName("rotation");
foreach (XmlNode transformInfo in transformList)
{
XmlNodeList transformcontent = transformInfo.ChildNodes;
foreach (XmlNode transformItens in transformcontent)
{
if(transformItens.Name == "x")
{
Debug.Log(transformItens.InnerText);
resultatFraXML += transformItens.InnerText;
}
if(transformItens.Name == "y")
{
Debug.Log(transformItens.InnerText);
resultatFraXML += transformItens.InnerText;
}
if(transformItens.Name == "z")
{
Debug.Log(transformItens.InnerText);
resultatFraXML += transformItens.InnerText;
}
if(transformItens.Name == "name")
{
resultatFraXML += transformItens.InnerText;
guitext.text = resultatFraXML;
GameControl.control.outputXml = resultatFraXML;
Debug.Log(resultatFraXML);
}
}
}
}
}
The error I get is :
XmlException: Document element did not appear. file:///var/mobile/Applications/47C26549-9CB0-46C2-9906-66399A5E515C/Documents/gamexmldata.txt Line 1, position 1.
at Mono.Xml2.XmlTextReader.Read () [0x00000] in <filename unknown>:0
at System.Xml.XmlTextReader.Read () [0x00000] in <filename unknown>:0
at Mono.Xml.EntityResolvingXmlReader.Read () [0x00000] in <filename unknown>:0
at Mono.Xml.DTDValidatingReader.ReadContent () [0x00000] in <filename unknown>:0
at Mono.Xml.DTDValidatingReader.Read () [0x00000] in <filename unknown>:0
at Mono.Xml.Schema.XsdValidatingReader.Read () [0x00000] in <filename unknown>:0
at System.Xml.XmlValidatingReader.Read () [0x00000] in <filename unknown>:0
at System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0
at System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0
at System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) [0x00000] in <filename unknown>:0
at System.Xml.XmlDocument.Load (System.String filename) [0x00000] in <filename unknown>:0
at ReadXmlData.LoadFromXml () [0x00000] in <filename unknown>:0
at ReadXmlData.Start () [0x00000] in <filename unknown>:0
(Filename: Line: -1)
I was thinking it might be UTF-8 and BOM problems, but I have done all converting and checking etc. Can´t find any BOM at all.
By accident the same error came when I played around with the Unity editor and then it appeared that the file was empty. Earlier on my debugging I did a File.Create in the code. Could it be that I have a empty file in the app that has NO data in it all? Or is the file placed in a wrong directory?
Any good ideas to why I get this kind of error?
Yes, I reckon your X$$anonymous$$L file contains no text. The reason I say this, is that if I work backwards through the call stack I can see your Start()
calls LoadFromX$$anonymous$$L()
, which calls Load
, then the xml reading code drills down and ends up in $$anonymous$$ono.Xml2.XmlTextReader.Read()
. The error message says that row 0, column 0 was where the problem happened, so I guess that no data could be read.
if(!System.IO.File.Exists(Application.persistentDataPath+"/PlayerData.xml"))
{
//copy resource file to persistent path
System.IO.File.WriteAllText(Application.persistentDataPath+"/PlayerData.xml",textX$$anonymous$$L.text);
xmlP.Load(Application.persistentDataPath+"/PlayerData.xml");
}
else
{
xmlP.Load(Application.persistentDataPath+"/PlayerData.xml");
}
textX$$anonymous$$L is public textAsset filled with resource folder file
Answer by Tasmia · Apr 10, 2014 at 10:21 AM
string path = path = Application.persistentDataPath + "/gamexmldata.xml";
replace your extension and try again
Your answer
Follow this Question
Related Questions
iOS PlayerPrefs issue, Working on Desktop Not on iOS device (5.1) 0 Answers
how can i take a screenshot (by UiButton) and save it in CameraRoll (Gallery in iOS)? 0 Answers
Saving My World 1 Answer
the XML file doesn't work when i install the app from the app store 0 Answers
Write and Read to a iOS device! Help 1 Answer