• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Yaza · Sep 26, 2015 at 10:04 AM · androidloadxmldataapp

Load Xml on Android with XmlReader

Hello,

I'm developing an Android app at the moment and need to load unit data. I chose xml files to store this data, and it works perfectly fine on PC. On Android it doesn't work and I guess the reason for that is the way I load the xml files frm the resource folder:

 XmlReader reader = XmlReader.Create(Application.dataPath + "/Resources/XML/Units/" + Dicts.UnitTypeToString(unitType) + ".xml");
         xmlDoc = new XmlDocument();
         xmlDoc.Load(reader);
         reader.Close();

Do you know why this doesn't work? Or should I load my Xml files differently?

Thanks for your help!

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by vladrybak · Sep 26, 2015 at 11:51 AM

This is how i save and load my class PluginParameters

Save File:

 XmlSerializer serialWrite = new XmlSerializer( typeof(PluginParameters));
 Stream stream = new FileStream(Application.dataPath + "/Resources/PluginParams/plugin_config.xml",   FileMode.Create , FileAccess.Write );
 serialWrite.Serialize (stream, pluginParameters);
 stream.Close();

Load file:

 XmlSerializer parametersSerializer = new XmlSerializer(typeof(PluginParameters));
 Stream reader = new MemoryStream( (Resources.Load("PluginParams/plugin_config", typeof(TextAsset)) as TextAsset).bytes );
 StreamReader textReader = new StreamReader(reader);
 pluginParameters = (PluginParameters) parametersSerializer.Deserialize( textReader );
 reader.Dispose();
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yaza · Sep 26, 2015 at 06:39 PM 0
Share

Thank you, so I guess loading the xml as a TextAsset and converting it to an XmlDocument later might be a solution to my problem. I still wonder why my method doesn't work though.

avatar image

Answer by Deranged-Mind · Dec 16, 2015 at 08:34 AM

I've just been struggling with this problem as well. It turns out that in order to read XML data on an Android device, you need to load the TextAsset from your Resources folder.

  1. Create a folder in your Assets folder named Resources and place your XML file into that folder.

  2. Load the TextAsset using Resources.Load(). Use the name of the file without adding the extension (e.g., "firstlevel") and cast it as a TextAsset.

  3. Create an XmlSerializer for your class (my class type was Map, yours will probably be different)

  4. Then create a stream for your code to Deserialize your XML code.

    TextAsset levelXML = (TextAsset)Resources.Load("FileNameWithoutExtension", typeof(TextAsset));

    XmlSerializer serializer = new XmlSerializer(typeof(Map));

    using (var stream = new MemoryStream(levelXML.bytes)) { map = (Map)serializer.Deserialize(stream); }

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deranged-Mind · Dec 16, 2015 at 01:08 AM 0
Share

Doesn't look like my code wants to format correctly, but if you're still having problems, lemme know! :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load from Xml on Iphone and Android 2 Answers

XML Loading works in PC... but not in Android 2 Answers

Save and Load data from Local XML file won't work 1 Answer

The best way to save and load data for mobile in unity ? 1 Answer

How to load XML on Android 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges