Talk:Saving and Loading Data: XmlSerializer

From Unify Community Wiki
Jump to: navigation, search

Just adding this, because I realized that saving in ANSI by default isn't always preferable. For my case, I discovered that the Korean codepage is not supported, so I could write the out XML file no problem, but it would not load. My solution was to change the serialization to use an XmlTextWriter and write the file out using UTF-8, no changes were required for loading.


public void Save(string path)
    var serializer = new XmlSerializer(typeof(YourContainerObjectClass));
    using (var stream = new FileStream(path, FileMode.Create))
        var xmlWriter = new XmlTextWriter(stream, Encoding.UTF8);
        serializer.Serialize(xmlWriter, this);


NullReferenceException on iOS Devices

I experienced NullReferenceExceptions when using the XmlSerializer on my iOS device, no error appeared in Unity. The fix for this was to not use a List<T> in the MonsterContainer, I used an array instead, and to remove { get; set; } from the class I was serializing.

- Marcus

Personal tools