Loading XML file from resources file after Build

I have an xml file that stores dialog used for my game. This file is stored inside of the resources folder and is read in by specifying the exact path name: "XmlData\strings"

This works just fine when running through the editor, but when I try to run after I built the project, it doesn't work.

I have found a few solutions that say to append .dll to the .xml file and load it in using dllimport, but I still can't get this to work. Does anyone have a good solution for reading in this .xml file from the built resources directory?

The solution is to use Ressources.Load(filename without extention)

e.g. to load your XML file from inside the Ressource Folder

// if your original XML file is located at
// "Ressources/MyXMLFile.xml"
TextAsset textAsset = (TextAsset) Resources.Load("MyXMLFile");  
XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml ( textAsset.text );

Hope that helped you loading your XML file.


Not really a solution to read from the built resource ( I am not sure if you actually know the difference between the Resource folder and the project resources that are made available automatically by unity).

Check this link first: This might be your issue:

Instead of storing it in the Resource folder or anything fancy, simply declare a public var in your script interested by this xml file:

public var xmlDefinition:TextAsset;

And in the inspector, simply drag and drop the xml file onto the "xmlDefinition" field now available.

Then it will be included during publishing now.

If I misunderstood your problem, can you elaborate more on how you actually have it to work within the editor? what function to you use to read the xml.



Hi there,
If you encounter the Utf-8 BOM error (line 1 problem) here might be the solution for you:
(the whole static class)

using UnityEngine;
using System;
using System.Xml;
using System.IO;

public static class RyXmlTools
    public static XmlDocument loadXml(TextAsset xmlFile)
        MemoryStream assetStream = new MemoryStream(xmlFile.bytes);
        XmlReader reader = XmlReader.Create(assetStream);
        XmlDocument xmlDoc = new XmlDocument();
        catch (Exception ex)
            Debug.Log("Error loading "+ xmlFile.name + ":

" + ex);
Debug.Log(xmlFile.name + " loaded");

        return xmlDoc;

    public static void writeXml(string filepath, XmlDocument xmlDoc)
        if (File.Exists(filepath))
            using (TextWriter sw = new StreamWriter(filepath, false, System.Text.Encoding.UTF8)) //Set encoding

Keep in mind that you can only write to the text asset in edit mode - AND that the xml file has to be in resources - so you can load it at runtime…
here a short example how to use it:

To load the XmlDocument TextAsset:

TextAsset m_XmlTextAsset = (TextAsset)Resources.Load("FileNameWhitoutFileExtention", typeof(TextAsset));
XmlDocument m_xmlDoc = RyXmlTools.loadXml(m_XmlTextAsset);

And to save it:

private static string m_XmlTextAssetPath = Application.dataPath.ToString() + "/resources/FileName.xml";
RyXmlTools.writeXml(m_XmlTextAssetPath , m_xmlDoc);

I hope this helps…

After the build, put that folder as you said: Resources \ XmlData \ strings in the same directory as the executable or does not work inside the folder: name of the game _data

Hope this helps ..

Please could you pass me the script to read information from an XML? and how to tell the GUI that will read a specific line of XML?

Thanks ...

I’m not 100% sure what your problem is but If what you want to do is load in an xml file after build than you just need to use Application.datapath + “filename.xml”. Application.datapath will return the path to the assets folder while in the Editor and the path to the data folder when running a windows build. Also will get you the correct path on whatever other device you build it on.

Resource Folders must be added in the editor and can’t be added after build time.

Hope that helped.

There’s already a working solution, but you might want to try using Unity’s XML serializer, because it lets you access XML data much easier.
It let’s you load an XML as if you were loading an object, and you can directly access the nodes in your XML like a normal C# variable.

Here’s how:

Here’s what I use:

    public T LoadXmlFromResources<T>(string filename)
        TextAsset xmlAsText = (TextAsset)Resources.Load(filename);

        MemoryStream stream = new MemoryStream(xmlAsText.bytes);

        XmlSerializer serializer = new XmlSerializer(typeof(T));
        T xmlDeserialized = (T)serializer.Deserialize(stream);


        return xmlDeserialized;

Let’s say I have an XML node called “VariableBeneathRoot”. Then I can simply call on the returned object: MyXMLObject.VariableBeneathRoot et voila