我正在尝试读/写一个xml文件来存储我游戏的一些数据。这是我想要存储的对象:
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;}
}
SpeedRecord的位置:
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是:
public struct SpeedSegment
{
public float speed;
public float time;
}
最后GPSPoint是:
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;
}
}
我遇到的问题是除了iOS之外,所有平台上的一切都能完美运行。我可以在所有平台上生成我的XML文件而没有任何问题,但是当我尝试在iOS上阅读它们时,我收到以下错误:
XmlException:未显示文档元素。第1行,位置1.
在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
有关可能导致此问题的任何建议吗?
我猜这是特定于iOS的东西,因为该解决方案在Android,Standalone和编辑器内工作正常。
编辑 - 这是我要解析的XML文件:
http://hastebin.com/tilareququ.xml(太长时间直接粘贴)
最后,这是我用来序列化/反序列化的方法:
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);
}
}
答案 0 :(得分:1)
我有根据的猜测是,您在文档中包含了BOM(Unicode字节顺序标记)。这些是3个字节,用于指定文件的编码。 BOM写在XML文件的开头。因此,在使用XMLTextReader读取文件时,它会抱怨文档没有以元素开头,因为而是读取了BOM。
我的建议是指定在编写XML文件时不要写BOM。您可以通过将false传递给UTF8Encoding:new System.Text.UTF8Encoding(false);
示例:
XmlTextWriter xmlWriter = new XmlTextWriter(streamReader, new System.Text.UTF8Encoding(false));
//now the BOM is not written to your XML file
答案 1 :(得分:0)
您是否尝试过使用StreamReader而不是File.Open?StreamReader re = new StreamReader(fileName, System.Text.UTF8Encoding);