- Home /
Other
Loading/Saving XML not working on Android
I am attempting to read in data from an XML file, modify that data, then save the file. The following code works perfectly fine on PC, but does not work when I test it on Android. Can anyone explain where I've gone wrong?
On Android, the filepath string ends up being: /data/data/com.unity3d.player/files/save_data.xml However, if I do a check to see if a file exists at that location it comes back false.
public void WriteStarGemData(string gem)
{
string filepath = Application.persistentDataPath + "/save_data.xml";
Debug.Log(filepath);
XmlDocument xmlDoc = new XmlDocument();
if (!File.Exists(filepath)) //if the file does not exist at Application.persistentDataPath, create it
{
xmlDoc.Load(Application.dataPath + "/Resources/save_data.xml");
xmlDoc.Save(filepath);
}
else
{
xmlDoc.Load(filepath);
XmlNodeList nodelist = xmlDoc.SelectNodes("Root/STAR_GEMS/String"); //get a list of all the Star Gem nodes in the file
foreach (XmlNode node in nodelist)
{
if (node.Attributes["key"].Value == gem) //if supplied string matches the key, swap it's value
{
if (node.Attributes["value"].Value == "false")
node.Attributes["value"].Value = "true";
else
node.Attributes["value"].Value = "false";
break;
}
}
xmlDoc.Save(filepath);
}
}
Hi,I come across the same problem just like yours. Could I know how you figured it out ,please? I run the program well on PC,but it doesnt work on Android. I really need your help. Thank you very much.
try to add xml file in Resources folder and load xml file using this code.
TextAsset textAsset = (TextAsset)Resources.Load("xml file name without extension", typeof(TextAsset));
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(textAsset.text);
This appears to be a duplicate of: http://answers.unity3d.com/questions/203852/how-to-access-persistent-data-path-in-android-phon.html?redirectedFrom=516584
But that post is pretty old and hasn't marked the answer as correct. Could you please try that top solution, and if it works, come back and post the answer/mark yourself as correct?
Follow this Question
Related Questions
Android Persistant Data 1 Answer
Load file on Android 1 Answer
xmlDocument.loadXml/load raises NotImplementedException on android, works in editor 1 Answer
Android and XML (Reading/Wirting) 1 Answer
android xml language issue 1 Answer