- Home /
XML Loading works in PC... but not in Android
Hello everybody,
When I run my application in the Editor everything works fine. I load an XML this way:
xmlPreguntas.Load(Application.dataPath.ToString() + "/Resources/xxxxxxx.xml");
Then, I compile to Android, but hte XML document doesn't load.
I was not sure why was this happening, but I read somewhere something that should work for me, loading by Resources.Load(). I tried that way typing this:
TextAsset textAsset = (TextAsset) Resources.Load("xxxxxx.xml");
XmlDocument xmlPreguntas = new XmlDocument();
xmlPreguntas.LoadXml(textAsset.text);
This code, instead, doens't work nor in the Editor. The error shown is the next: NullReferenceException: Object reference not set to an instance of an object, refering the last line. I thinks it's telling me that the loading is failing, but I don't really know why.
Hope there's somebody smarter than me able to help me.
Greetings,
Alex.
Load wotks is great! but TextAsset is only read how i can load and save xml file?
Resources is read-only. So write the X$$anonymous$$L out to the file system using StreamWriter.
Answer by liszto · Jan 09, 2013 at 04:09 PM
do you try to load your xml with the second method but without the extension ?
Something like this :
TextAsset textAsset = (TextAsset)Resources.Load("FileNameWhitoutFileExtention", typeof(TextAsset));
XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml ( textAsset.text );
oh! When I first saw that "without extension" i thought in the path. Let me try!
how can i do it with a relative path?? I don't want the xml file in the resources folder. help!
This worked for me.. but I initially missread it. In your resource folder keep the .xml extension on your file name but in your code accessing it leave the extension off. It will auto find the .xml. I erased the xml extension from my file name and had a lot of grief trying to figure out why this did not work.
Answer by wildikon · May 12, 2014 at 12:30 PM
Hi,
having the same issue loading XML (works on PC but not on my android device)
When I use the code |
TextAsset textAsset = (TextAsset)Resources.Load("FileNameWhitoutFileExtention",typeof(TextAsset));
I keep getting an error, saying line (63,18): UCE0001: ';' expected. Insert a semicolon at the end
??? this is driving me a little bit insane, what am I missing here?
thanks for any help offered!
Answer by koslovdenis · Mar 08, 2019 at 12:56 PM
I can't seem to work it out in 2019, in unity 2018.3. ANd i can't understand why... Although I am trying to access my .xml file with GetElementById() method. My curr_file_name string variable is assigned runtime in the code (my game has 2 languages so I need to load different files) and it doesn't have an extention.
TextAsset textAsset = (TextAsset)Resources.Load(curr_file_name, typeof(TextAsset));
XmlDocument doc = new XmlDocument();
doc.LoadXml(textAsset.text);
XmlElement greeting_word_id = doc.GetElementById("greeting_word");
greeting_word = greeting_word_id.InnerText;
Anyway. I compiled this code and I opened an APK file with zip. No xml files were there. What am I doing wrong? Please if anyone could work it out tell me HOW? I am struggling with this over a few weeks in a row... and I am getting really crazy. Thank you..
@koslovdenis were you able to solve the problem as I have the same issue now. Kindly help someone. The game loads well in editor but not on the build apk. What am I doing wrong. I used the same code above.
Please don't necro post on such an ancient question. Especially you have commented on an answer that is not an answer but also a question. koslovdenis hasn't been online since end of March 2019, so it's unlikely he will respond. Also his answer-question doesn't really say what doesn't work. Of course there will be no xml file inside the apk since TextAssets are assets and are packed into asset files which form Unity's asset database. Who knows what actual issue he had.
If you have a problem or a question, please ask your own question. When you do so, add more information. What have you done, what did you expect to happen, what happens instead, What observations have you made, ....
I have already asked a question on this. And didn't get any working answer. So I figured since this worked for others I might as well ask here. You might as well help with $$anonymous$$e using the link below. https://answers.unity.com/questions/1854198/i-have-a-quiz-game-that-loads-questions-using-an-x.html.
Your answer
Follow this Question
Related Questions
Load from Xml on Iphone and Android 2 Answers
build android apk that contain Resource XML 0 Answers
Load Xml on Android with XmlReader 2 Answers