- Home /
Problem with XML Serialization in Android (XMLException)
I am using the code from The blog of Simon Wittber to save and load a class in XML. It works fine in Mac/Win and iOS, but just in Android I get following error:
XmlException: Text node cannot appear in this state. Line 1, position 1. Mono.Xml2.XmlTextReader.ReadText (Boolean notWhitespace) Mono.Xml2.XmlTextReader.ReadContent () Mono.Xml2.XmlTextReader.Read () System.Xml.XmlTextReader.Read () System.Xml.XmlDocument.ReadNodeCore (System.Xml.XmlReader reader) System.Xml.XmlDocument.ReadNode (System.Xml.XmlReader reader) System.Xml.XmlDocument.Load (System.Xml.XmlReader xmlReader) System.Xml.XmlDocument.LoadXml (System.String xml)
I don't think UTF-8 (and BOM) would be the issue here since the code is explicitly using ASCIIEncoding.
here is the code of StoreObject.cs Class that I use:
using UnityEngine;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
//using PlayerPrefs = PreviewLabs.PlayerPrefs;
public class StoreObject
{
public static void Save<T> (string name, T instance)
{
XmlSerializer serializer = new XmlSerializer (typeof(T));
using (var ms = new MemoryStream ()) {
serializer.Serialize (ms, instance);
PlayerPrefs.SetString (name, System.Text.ASCIIEncoding.ASCII.GetString (ms.ToArray ()));
}
//PlayerPrefs.EnableEncryption(true);
//PlayerPrefs.Flush ();
}
public static T Load<T> (string name)
{
if(!PlayerPrefs.HasKey(name)) return default(T);
XmlSerializer serializer = new XmlSerializer (typeof(T));
T instance;
using (var ms = new MemoryStream (System.Text.ASCIIEncoding.ASCII.GetBytes (PlayerPrefs.GetString (name)))) {
instance = (T)serializer.Deserialize (ms);
}
return instance;
}
}
Thanks. Edit: Can Anyone help?