简体   繁体   中英

XML Serialization in Unity iOS exception

I am trying to read/write an xml file to store some data for my game. Here is the object i am trying to store:

public struct EndRaceData
{
        // Data used to display section by section data
        public List<SpeedRecord> speedRecords { get; set; }
        // Data used for making a map
        public List<KeyValuePair<String,SpeedRecord>>recordsByname { get; set; }
        // Taken from Datatracker.instance
        public List<KeyValuePair<String,List<DataRecorder.GPSPoint>>> pointsPerChallenge { get; set; }
        // Taken from DataRecorder.instance
        public List<SpeedSegment> averageSpeeds { get; set; }
        // Taken from IPlayer..
        public float caloriesburnt { get; set; }
        // Taken from Datatracker.instance
        public float topSpeed { get; set; }
        // taken from Datatracker.instance
        public float points { get; set; }
        // taken from Datatracker.instance
        public string difficulty { get; set; }
        // taken from Datatracker.instance
        public float completedPercentage { get; set; }
        // challenge-specific data
        public int passedChallenges { get; set; }
        // Datatracker.instance
        public int failedChallenges { get; set; }
        // Datatracker.instance
        public int  fastChallengesPassed { get; set; }
        // Datatracker.instance
        public int slowChallengesPassed { get; set; }
        // Datatracker.instance
        public int keepChallengesPassed{ get; set; }
        // Datatracker.instance
        public string startTime { get; set; }
        // Datatracker.instance
        public string endTime   { get; set; }


        public float distanceTravelled { get; set; }

    public string filePath {get; set;}
}

Where SpeedRecord is:

public struct SpeedRecord
{
        public float speed;
        public float distance;
        public string name;
        public string sectionStartTime, sectionEndTime;

        public SpeedRecord (string name, float speed, float distance, DateTime startTime, DateTime endTime)
        {
                this.speed = speed;
                this.name = name;
                this.distance = distance;
                this.sectionStartTime = startTime.ToString ();
                this.sectionEndTime = endTime.ToString ();
        }

        public override string ToString ()
        {
                return string.Format ("{0}\r\nAVG speed : {1} km/h\r\nDist : {2} m\r\nTime : {3} -> {4}\r\n", this.name, this.speed, this.distance, this.sectionStartTime, this.sectionEndTime);        
        }
}

SpeedSegment is:

public struct SpeedSegment
{
        public float speed;
        public float time;
}

and finally GPSPoint is:

public struct GPSPoint
    {
            public float lat;
            public float lon;
            public float accuracy;
            public float time;

            public GPSPoint (float lat, float lon, float acc, float time)
            {
                    this.lat = lat;
                    this.lon = lon;
                    this.accuracy = acc;
                    this.time = time;
            }
    }

The issue I am having is that everything works perfectly on all platforms except for iOS. I can generate my XML files without any issues on all platforms, but when I try to read them on iOS, I get the following error:

XmlException: Document element did not appear. Line 1, position 1.
at Mono.Xml2.XmlTextReader.Read () [0x00000] in :0 at System.Xml.XmlTextReader.Read () [0x00000] in :0 at System.Xml.XmlReader.MoveToContent () [0x00000] in :0 at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00000] in :0 at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00000] in :0

Any suggestions as to what might be causing this?

Im guessing it is something specific to iOS since the solution works fine on Android, Standalone and inside the editor.

Edit - Here is the XML file i am trying to parse:

http://hastebin.com/tilareququ.xml (Too long to paste directly)

Finally, here is the methods I use to serialize/deserialize:

    public static void WriteXML (string fileName, object obj)
    {
            using (var f = File.Create (fileName)) {
                    XmlSerializer ser = new XmlSerializer (obj.GetType ());
                    ser.Serialize (f, obj);
            }
    }

    public static T ReadXML<T> (string fileName)
    {
            using (var f = File.Open (fileName, FileMode.Open, FileAccess.Read)) {
                    XmlSerializer ser = new XmlSerializer (typeof(T));
                    return (T)ser.Deserialize (f);
            }
    }

My educated guess is that you included the BOM (Unicode Byte-Order Mark) in your document. These are 3 bytes that specify the encoding of the file. The BOM is written at the beginning of the XML file. Therefore, when reading the file with XMLTextReader, it complains that the document didn't start with an element, because the BOM was read instead.

My advice would be to specify that you don't want the BOM to be written when writing XML files. You can do this by passing false to UTF8Encoding: new System.Text.UTF8Encoding(false);

Example:

XmlTextWriter xmlWriter = new XmlTextWriter(streamReader, new System.Text.UTF8Encoding(false));
//now the BOM is not written to your XML file

Have you tried to use StreamReader rather than File.Open? StreamReader re = new StreamReader(fileName, System.Text.UTF8Encoding);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM