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?

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);


    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);

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.

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: