Hello, I want to read a XML file in Android but I can’t find the XML file in APK file. When I build the app I get a error: NotSupportedException: The URI prefix is not recognized.
Filepath is: Assets/StreamingAssets/Diller/ceviri-tr.xml
Code:
void Start()
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.streamingAssetsPath + "/Diller/ceviri-tr.xml");
}
You can’t directly access streaming assets with normal File I/O as those assets are not actual files in the file system but are packed inside your APK file. You can read about that in the StreamingAssets manual page.
On platforms like Android and WebGL you can only read the streaming assets by using a UnityWebRequest and provide it with a special URI that is returned by Application.streamingAssetsPath.
You have to use the “LoadXml” method instead of “Load” and pass it the content of the xml file which you loaded with the UnityWebRequest. Note that the streaming assets are readonly on Android and WebGL builds. If you want to be able to modify the file, you have to copy the file to the persistent data path if the file does not exist yet and work on that copy instead.
Hello, thanks for you answer now I can read XML file but I still get a error: XmlException: Data at the root level is invalid. Line 1, position,1.
Code:
void Start()
{
var webrequest = UnityWebRequest.Get(Application.streamingAssetsPath + "/Diller/ceviri-tr.xml");
webrequest.SendWebRequest();
doc.LoadXml(webrequest.downloadHandler.text);
}
XML file:
<ceviri>
<startbutton>Başla</startbutton>
<levels>Seviyeler</levels>
<menu>Menü</menu>
<mermi>Mermi: </mermi>
</ceviri>