我有一个奇怪的问题反序列化,并想知道是否有人可以阐明它。抱歉粗略的代码,但这是一个原型。
基本上我正在尝试序列化和反序列化一个简单的类:
[Serializable]
[Preserve(AllMembers=true)]
public class School
{
public School ()
{
}
public string est_name{get; set;}
public string postcode{get; set;}
public string phase {get; set;}
public string head_name{get; set;}
public string urn {get; set;}
public long distance{get; set;}
public string coord{get; set;}
public string gender{get; set;}
public int totpup{get; set;}
public int totelig{get; set;}
public float pmattest14p{get; set;}
public float pmattest15{get; set;}
public float pengtest14p{get; set;}
public float pengtest15{get; set;}
public float apsengmattest{get; set;
}
序列化的代码如下,使用School []作为T,它是来自泛型类的方法:
public void serializesample(T sample)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Sample2.xml") Stream st = new FileStream(path, FileMode.OpenOrCreate);
XmlWriter w = new XmlTextWriter(st, Encoding.UTF8);
serializer.Serialize(w, sample);
st.Flush();
st.Close();
}
生成以下XML文件:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSchool xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<School><est_name>testName</est_name><postcode>N7 0NA</postcode><head_name>thedude</head_name><distance>0</distance><gender>mixed</gender><totpup>0</totpup><totelig>0</totelig><pmattest14p>0</pmattest14p><pmattest15>0</pmattest15><pengtest14p>5</pengtest14p><pengtest15>3</pengtest15><apsengmattest>0</apsengmattest></School><School><est_name>testName2</est_name><postcode>N7 4NA</postcode><head_name>thedude</head_name><distance>0</distance><gender>mixed2</gender><totpup>0</totpup><totelig>0</totelig><pmattest14p>0</pmattest14p><pmattest15>0</pmattest15><pengtest14p>5</pengtest14p><pengtest15>3</pengtest15><apsengmattest>0</apsengmattest></School></ArrayOfSchool>
反序列化代码如下,School []为T:
private T ReadObject(XmlReader reader)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(reader);
}
XmlReader r = XmlReader.Create(new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Sample2.xml"), FileMode.Open));
e.responseObject = ReadObject(r);
我在最后列出的行“responseObject = ReadObject(r);”中继续得到以下异常:
System.InvalidOperationException:XML文档中存在错误。 ---&GT; System.InvalidOperationException :(未知)未被预期
在/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs中的System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot(System.Xml.Serialization.XmlTypeMapping rootMap)[0x00050] :182
在/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87中的System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot()[0x00028]中
在/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs中的System.Xml.Serialization.XmlSerializer.Deserialize(System.Xml.Serialization.XmlSerializationReader reader)[0x0001c]中:361
---内部异常堆栈跟踪结束---
在/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs中的System.Xml.Serialization.XmlSerializer.Deserialize(System.Xml.Serialization.XmlSerializationReader reader)[0x00061]中:366
在/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:350中的System.Xml.Serialization.XmlSerializer.Deserialize(System.Xml.XmlReader xmlReader)[0x0002c]中
在Burnspeed.Utilities.WebRequestHelper 1[SchoolFinder_Prototype.School[]].ReadObject (System.Xml.XmlReader stream) [0x00010] in /Users/Khalil/Desktop/Monotouch Utility/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs:201
at Burnspeed.Utilities.WebRequestHelper
1 [SchoolFinder_Prototype.School []]。/ Users / Khalil / Desktop / Monotouch Utility / Burnspeed.Utilities / Burnspeed.Utilities / WebRequestHelper.cs中的ProcessHttpResponseAndFire(IAsyncResult iar)[0x000ae] :156
答案 0 :(得分:1)
我不完全确定为什么会发生这种异常。我尝试了你的代码只做了很少的修改,它的工作原理。我没有使用[Preserve(AllMembers = true)],我将每个T替换为一个School [](你也做了,不是吗?)我替换了行“e.responseObject = ReadObject(r) “,你的异常发生的地方,”School [] newArray = ReadObject(r)“。不想编写其余代码而不修改这些小位。
您可以将方法更改为
,而不是将每个T替换为School [] public void serializesample<T>(T sample) {
XmlSerializer serializer = new XmlSerializer(typeof(T));
string path = "G:\\sample.xml";
Stream st = new FileStream(path, FileMode.OpenOrCreate);
XmlWriter w = new XmlTextWriter(st, Encoding.UTF8);
serializer.Serialize(w, sample);
st.Flush();
st.Close();
}
private T ReadObject<T>(XmlReader reader) {
XmlSerializer serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(reader);
}
并通过serializesample<School[]>(array)
和ReadObject<School[]>(r)
调用它们。
我只是不确定你在代码中用T做了什么,我猜问题可能就在那里。希望有所帮助,即使异常来自Monotouch [Preserve(AllMembers = true)]也可能无济于事。