- Home /
Something should I know about how to load XML on runtime game?
I'm developing a little framework to help me out how to handle some 2D stuffs on the scene, like a tilemap, camera control, some helpers and other stuffs to manipulate 2D games.
To approach that I'm storing some data into XML files, so I can easily do that like it below and works very well on editor script or something like that:
public static T DeserializeXml<T>(this string xml) where T : class
{
if (xml == null) return null;
var s = new XmlSerializer(typeof(T));
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
return (T)s.Deserialize(stream);
}
}
public static void SerializeXml<T>(string path, T instance) where T : class
{
if (instance == null) return;
var s = new XmlSerializer(typeof(T));
//using (TextWriter stream = new StreamWriter(path, false, Encoding.UTF8))
using (var stream = new FileStream(path, FileMode.Create))
{
s.Serialize(stream, instance);
stream.Dispose();
stream.Close();
}
}
However I wonder about how to make it possible (save and load) XML files on execution time regardless the platform it is running. Unity documentation is unclear, so there is some approaches that make it possible but other don't.
Questions:
Have I need to put all XMLs in a "Resources" folder to make it visible regardless the platform?
Have I need to use Application.streamingAssetsPath and store all XMLs in a "StreamingAssets" folder to make it possibile?
Or maybe I need to make the both solutions above to turn it possible?
Something else completely different?
I'm confused what I have to do to make it possible for all situations.
Thanks in advance, guys!
Answer by DJT4NN3R · Aug 22, 2017 at 09:14 AM
I think the solution is to create a string that returns the path depending on which platform you're running on. For example:
string path
{
get
{
if (// on Windows platform) return // Windows path;
if (// on Android platform) return // Android path;
}
}
You would then feed this to your FileStream when trying to deserialize.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082840im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Read AND Write to XML at runtime 1 Answer
c# to compile xml string as code during runtime 1 Answer
XmlException: unexpected end of file. Current depth 1 Answer
WebGL XML parsing error "InvalidOperationException" 0 Answers
Getting data from XML 2 Answers