- Home /
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?
Answer by user-3451 (google) · May 09, 2011 at 10:58 AM
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.
This worked for me using JsonUtility.FromJson(textAsset.text) which I had generated and saved using a binary formatter, though I had to open my text file and clear the BO$$anonymous$$ heading using notepad++
Answer by Jean-Fabre · Apr 20, 2011 at 06:02 AM
Hi,
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.
Bye,
Jean
Answer by methotec · May 31, 2012 at 04:13 PM
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;
[ExecuteInEditMode]
[Serializable]
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();
try
{
xmlDoc.Load(reader);
}
catch (Exception ex)
{
Debug.Log("Error loading "+ xmlFile.name + ":\n" + ex);
}
finally
{
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
{
xmlDoc.Save(sw);
}
}
}
}
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...
Answer by Unamine · May 09, 2011 at 11:56 AM
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 ...
Answer by Kevin Ambruster · Sep 14, 2011 at 04:27 AM
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.
This worked fine for me but only if, after running the build, I copy the xml file and everything else from the project/Assets/Resources folder (some prefabs eg) manually to the data/ Resources folder. What's wrong with my resources folder that files/prefabs aren't included?