- Home /
Read XML from Application.persistentDataPath
Ok so I know how to read an xml from the projects internal path - 'Resources' folder using :
public TextAsset levelFile;
private XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml (levelFile.text);
But I need to load an XML from an external path in android. So far I've done this :
if (System.IO.File.Exists (Application.persistentDataPath + "/levelFile.xml")) {
TextAsset level = (TextAsset)System.IO.File.ReadAllText(Application.persistentDataPath + "/levelFile.xml");
}
It returns an error :
error CS0030: Cannot convert type string' to
UnityEngine.TextAsset'
Any help appreciated. :)
Answer by CorruptedTNC · Apr 24, 2015 at 12:08 PM
A TextAsset is a file containing text which can be read from inside it. A string is the content of that file.
TextAsset level = (TextAsset)System.IO.File.ReadAllText(Application.persistentDataPath + "/levelFile.xml");
You can't just cast a string to a TextAsset, consider either accepting "level" as a string and using that, or if it's too difficult to do that, use an XMLReader (since that's what they're made for, after all).
Have a read through this MSDN article to get to grips with the basics of XMLReaders
Answer by digzou · Apr 24, 2015 at 01:37 PM
For people still looking for an answer I managed to do it with this code :
using System.Xml;
private XmlDocument xmldoc = new XmlDocument ();
string levelFilePath = Application.persistentDataPath + "/levelFile.xml";
if (System.IO.File.Exists (levelFilePath))
xmldoc.LoadXml (System.IO.File.ReadAllText(levelFilePath));
And you also have to change the Android player settings -> Other Settings -> Write access -> "External (SD card)".
This looks for the file in the path :
SDCard/Android/Data/com.yourgame.bundle/files/the_File_You_Want.
So I pasted my levelFile.xml in there after installing the apk.
Hope this helps. :)
Answer by koslovdenis · Mar 08, 2019 at 04:58 PM
Does it work on Android? It doesn't work for me.
Ok so I know how to read an xml from the projects internal path - 'Resources' folder using :
public TextAsset levelFile;
private XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml (levelFile.text);
I would like to know how to make it work. Please help. Thank you
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting the position of all game objects with the same tag 1 Answer
unity load an player made C# script 2 Answers