- Home /
Can't find XML file
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");
}
Answer by Bunny83 · Apr 10, 2021 at 01:36 PM
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.strea$$anonymous$$gAssetsPath + "/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>
Well, that's a different issue, but a common one ^^. The Xml parser does not like a BOM (Byte Order Mark) at the file start. Some text editors (especially notepad) adds a BOM even when the encoding is utf8 which does not need and does not recommend to add one. Anyways, the XmlParser can't handle the BOM. The best solution is to just remove the BOM from your file. Most text editors do support different encoding formats. Even notepad does support utf8 with and without BOM. So just open your file in notepad, click save as and choose just UTF8 as encoding (not with BOM) and then try again.
There are other solutions like pulling the bytes through a StringReader which should also remove the BOM as the BOM is not really a printable character.
Your answer

Follow this Question
Related Questions
How to override the default style.xml for a fullscreen android app? 1 Answer
Build app crashes on start because of including a provider in the AndroidManifest.xml 1 Answer
Unity 3d cross-platform data storage (standalone, android) 1 Answer
Will an Android Manifest file named "AndroidManifest 1.xml" file work? 1 Answer