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?
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();
}
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.