Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
4
Question by Ari Levi · Jun 21, 2011 at 05:37 AM · iphonexmlreadipod

How do I read XML on iPhone?

I have an XML file that I source data from. It works on my computer build, but not on iPhone.

From reading some other answers I get the idea that only certain directories can be read by the game on iPod/iPhone. How do I put the XML files in these directories and source the data?

Comment
Add comment · Show 1
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 zupermario · Mar 13, 2014 at 11:29 AM 0
Share

Hi everyone! Does some have a full working sample off this? Really would like to get going on saving a xml format on a ipad :)

Would really appreciate it :)

3 Replies

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

Answer by Herman-Tulleken · Jun 21, 2011 at 06:31 AM

Here is how to do it:

  1. Put your XML file in the resources folder in your project (that is, Assets/Resources/).

  2. Rename it so that it has .txt extension.

  3. In your code, use this to load the file as a TextAsset:

    TextAsset textAsset = (TextAsset)Resources.Load(filename, typeof(TextAsset));

  4. From here, you can get it into XMLDocument, if that is what you want.

    XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(new StringReader(textAsset.text));

If you want to serialise / deserialise classes into XML, the following utility functions will do the trick, as long as the path is somewhere in the resource folder, and files are named with the .txt extension:

 public static T DeserializeTextAsset<T>(string filename)
 {
     TextAsset textAsset = (TextAsset)Resources.Load(filename, typeof(TextAsset));

     if(textAsset == null)
     {
         Debug.LogError("Could not load text asset " + filename);
     }

     return DeserializeString<T>(textAsset.ToString());
 }

 //filename an object from an XML string.
 public static T DeserializeString<T>(string xml)
 {
     XmlSerializer serializer = new XmlSerializer(typeof(T));
     StringReader stringReader = new StringReader(xml);
     XmlTextReader xmlReader = new XmlTextReader(stringReader);
     T obj = (T)serializer.Deserialize(xmlReader);

     xmlReader.Close();
     stringReader.Close();

     return obj;
 } 

 public static void SerializeObject<T>(string filename, T data)
 {
     XmlSerializer serializer = new XmlSerializer(typeof(T));
     TextWriter textWriter = new StreamWriter(filename);
     serializer.Serialize(textWriter, data);
     textWriter.Close();
 }
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
3

Answer by Ari Levi · Jun 22, 2011 at 03:44 AM

This is how I bridged the gap. Thanks a lot for the help. Another friend told me about the middle step.

1.TextAsset textAsset = (TextAsset)Resources.Load(filename, typeof(TextAsset));

2.MemoryStream assetStream = new MemoryStream(textAsset.bytes);

3.XmlTextReader reader = new XmlTextReader(assetStream);

Then the rest of my code worked from there.

Comment
Add comment · Show 2 · 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 DanDixon · Nov 06, 2011 at 08:21 PM 0
Share

This is what worked for me (using a $$anonymous$$emoryS$$anonymous$$m). Thanks Ari Levi.

I haven't tested it on iOS, but on PC I did not find it necessary to rename the .xml file to .txt.

avatar image zupermario · Mar 13, 2014 at 10:51 AM 0
Share

Hi everyone!

Does anyone have a full working example of this? Would really appreciate it. Trying to save a xml formatet data on a ipad for days now.

:)

avatar image
2

Answer by pgomes · Apr 19, 2012 at 05:31 PM

A word of advice: When using System.Xml and deploying to iOS take care not to select ".NET 2.0 Subset" Api compatibility level in the player settings. I have experienced consistent crashes during runtime.

I'm assuming System.Xml uses some features not included in the subset. For a reference to these limitations see: http://docs.xamarin.com/ios/about/limitations

Comment
Add comment · 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 Shade_CZ · Aug 31, 2013 at 06:30 PM 0
Share

Nice! Works perfectly.

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

6 People are following this question.

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

Related Questions

Saving My World 1 Answer

android xml language issue 1 Answer

Using XML for a simple webplayer Highscore Table 1 Answer

BVH animation performance on iOS devices? 1 Answer

iPhone stripping errors? 0 Answers


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