- Home /
Android read/write .xml file from persistent-path
Thanks in advance for reading this question.
I'm having trouble reading/writting xml file on android devices. My game require read/write .xml file to function properly. So far i've successfully reading the xml content, but failed to overwrite/save the file. I've already set the Player Settings > Write Permission > External. I've also copied the .xml file to Android > data > com.myCompany.myTitle > files. This is some way that i tried to read the .xml file:
Using Streaming Datapath and www, i could read the xml content from www.text property
Using Persistent Datapath, i could check the file exist from Application.persistentDataPath + "/DataPlayer.xml". But,i cannot retreive its content.
Using Resources.Load ("Resources" folder), i could read the xml content. But it seems pre-compiled so the .xml file cannot be edited (please correct me if i'm wrong).
Using Public TextAsset and assign the xml through unity editor manually (Without "Resources" folder). Then Resources.Load could get its content just like method no.3
The way i'm reading the .xml file was by using XmlDocument with LoadXml(string content) method. Then, i modify the XmlDocument and finally call Save(string path) method. I used the Persistent Datapath as parameter, but what happened next was the xml file become corrupted (size become 0B). My attempt to overwrite the file has been unsuccessful so far.
Some discussion suggest using Persistent Datapath for deploying to mobile platform, but i can't seem to get it right. Others suggest doing serialize / deserialize, but i'm not quite understand the concept behind it.
Thank you again for reading all this. I hope someone could enlighten me on this matter.
Answer by Bieere · Aug 02, 2017 at 06:31 PM
If you're looking for an alternative solution to the XML document, assuming you're not attempting to change any Android specific files, try using JSON:
Essentially would be doing some of the same function as the XML from what i gather.
But back to your issue, it might be worth trying to use some of the function within System.IO to save the file as it does provide some functionality for overwriting files implicitly or manually
Thank you Bieere for your answer. I appreciate the JSON suggestion. Personally, i've never tried JSON so it's gonna took some time learning it. As of now, the project schedule on the tight spot so i'm gonna stick with X$$anonymous$$L for now. I'll test your suggestion using System.IO. I'll let you know when i've got the result.
I've tried your suggestions. The WriteAllText or AppendAllText didn't work at all. It didn't even trigger exception catch. $$anonymous$$eanwile, StreamWritter trigger exception IOException: Sharing violation on path. I've tried closing all program that access the file, but the process still corrupt the file into 0B. Both way were unsuccessful.
So from this point, i would suggest 2 more things to try.
Check out this link and see if these techniques for Reading/Writing help
Ensure that the External write permission is actually within the manifest
That link works for me! Thank you Bieere for your support these 2 days. However, upon successfully loading and saving the file, it still become 0B in size. I tried opening the xml file from PC. It opened but the edited part seems cropped out. From Android device explorer i check the xml file properties and it shows 0B in size. I tried opening it using default app from android, but it showed nothing, blank. $$anonymous$$eanwhile, through debugging the apps and checking InnerXml properties, it does correctly show the xml file content and all the edit that i've done. How come this happen?
Your answer
![](https://koobas.hobune.stream/wayback/20220612131749im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to rotate a 2D object (ex. log) with rigidbody2D based on touchPosition 1 Answer
How do you get the Standard Shader to work on iOs 9?, 2 Answers
Android Back (or Escape) button isn't being recognized. 3 Answers
How to make the back image appear before the front image where it is scratched? 0 Answers
Camera with Depth Only Clear Flags does not work on Mobile Devices 0 Answers