- Home /
Why might XML Serialization result in crashes?
I'm working on loading/saving some data with XML serialization (and I'm coding using Unity JavaScript). I've run into an issue in that the editor would frequently (but not always) crash when doing some of these serialization operations. However, when I sat down to try and debug why, it suddenly stopped crashing, and I haven't been able to get it to crash since.
Here is roughly what I've been trying to do. Assume I have a custom class similar to this:
class MyClass
{
var myVar;
// more vars here; they're all basic types like int, float, String, etc.
}
My save (serialize) and load (deserialize) routines look something like this:
Load:
var xmlSerializer : XmlSerializer = new XmlSerializer(typeof(MyClass));
var fileStream : FileStream = new FileStream(Application.dataPath + "/" + "data.xml", FileMode.Open);
myObject = xmlSerializer.Deserialize(fileStream);
Save:
var xmlSerializer : XmlSerializer = new XmlSerializer(typeof(MyClass));
var streamWriter : StreamWriter = new StreamWriter(Application.dataPath + "/" + "data.xml");
xmlSerializer.Serialize(streamWriter, myObject);
streamWriter.Close();
Assuming that both those save and load routines are wrapped in try/catch blocks to catch exceptions, is there any reason anything I'm doing here would cause the editor to crash? Is there anything else that I should be doing with XML serialization that I'm not doing?
Try adding error detection code (check for nulls etc.) If you're on Windows and you get a warning on startup about needing the service pack from a knowledge base article, then install it. If you're not on 3.3 then try an upgrade. $$anonymous$$y experience is that the editor does crash from time to time and you learn to live with it. :-p
Also after a crash you can check the previous editor log. In the Console window, at the top-right is a button called "open editor log", open that. If you are on mac you should see a list to the left displaying some logs, you should see a previous editor log. On PC i'm not sure if the previous editor log is even saved, try to find out where the log is saved and open that before you restart Unity next time it crashes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122650im_/https://answers.unity.com/themes/thub/images/avi.jpg)