Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
3
Question by digzou · Apr 24, 2015 at 07:18 AM · c#xmlpersistentdatapathexternal files

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. :)

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · 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
2

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. :)

Comment
Add comment · 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
0

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

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

22 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

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

Renderer on object disabled after level reload 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