Alright this is driving me nuts, here is my XML class
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEngine;
[System.Serializable]
[XmlRoot("UpdateData")]
public class UpdateData {
public float version;
//public string[] redownloadData;
public void Save(string path)
{
var serializer = new XmlSerializer(typeof(UpdateData));
using(var stream = new FileStream(path, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
public UpdateData Load(string path)
{
var serializer = new XmlSerializer(typeof(UpdateData));
using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
return serializer.Deserialize(stream) as UpdateData;
}
}
public UpdateData Load(StringReader xml)
{
var serializer = new XmlSerializer(typeof(UpdateData));
return serializer.Deserialize(xml) as UpdateData;
}
}
then in my main script I am doing:
updateData = updateData.Load(“C:/Projects/Unity3D/Game/Compiles/Windows/Game_Data/updateData.xml”);
running inside of the editor this work just fine, no issue. The xml file is there, it parses properly inside unity.
but I compile to standalone pc build and it doesn’t work!? I assume it can’t find the file… Is there some issue with using fstream in standalone win builds?
Btw I am hardcoding an explicit path just for testing purposes. Is the format used for windows standalone something different than that?
I think your problem is very simple : the xml file is not in your build. Did you add manually your file in your build folder ? (
For example, if your xml file is in : MyUnityProjects/Assets/XML_Data/YourXml.xml
You have to add : XML_Data/YourXml.xml in the root of your build (where the .exe file is)
When you build your game, Unity doesn’t include automatically your xml file (the only way to do that is to add a TextAsset variable to your class and assign your xml file by drag’n drop in the Editor)
no I added it manually… the path I explicitly hardcoded I did just for testing purposes and put the file there. 100% sure the file is there, just something about that first Load function I tried doesn’t work on standalone…
Old topic, but I faced the same problem, xml serialization works in editor mode, but doesn’t in standalone application. File path is not the issue, I also hardcoded it… Does anyone have a solution?
–
Found the reason. In my case it was because of this statement in xml:
encoding=“windows-1251”
The xml file was generated by saving data from unity, btw. And it was reading and writing in editor mode correctly…