- Home /
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?
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 :)
Answer by Herman-Tulleken · Jun 21, 2011 at 06:31 AM
Here is how to do it:
Put your XML file in the resources folder in your project (that is, Assets/Resources/).
Rename it so that it has .txt extension.
In your code, use this to load the file as a TextAsset:
TextAsset textAsset = (TextAsset)Resources.Load(filename, typeof(TextAsset));
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();
}
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.
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.
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.
:)
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
Your answer
Follow this Question
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