Serialization problem in monotouch
I'm having a strange problem deserializing and was wondering if someone could shed some light on it. Sorry for the rough code but this was meant as a prototype.
Basically I'm attempting to serialize and deserialize a simple class:
[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;
}
The code for serializing is as follows, using School[] as T, Its a method from a generic class:
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();
}
resulting in the following XML file:
<?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><d开发者_JAVA技巧istance>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>
The deserialization code is as follows, with School[] as 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);
I keep getting the following exception at the last listed line "responseObject = ReadObject(r);":
System.InvalidOperationException: There is an error in XML document. ---> System.InvalidOperationException: (unknown) was not expected
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x00050] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:182
at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00028] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x0001c] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:361
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x00061] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:366
at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader) [0x0002c] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:350
at Burnspeed.Utilities.WebRequestHelper1[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[]].ProcessHttpResponseAndFire (IAsyncResult iar) [0x000ae] in /Users/Khalil/Desktop/Monotouch Utility/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs:156
I am not completely sure why this exception happens for you. I tried your code with only small modifications and it works. I didn't use [Preserve(AllMembers=true)], I replaced every T by a School[] (which you did as well, didn't you?) and I replaced the line "e.responseObject = ReadObject(r)", where your exception happens, by "School[] newArray = ReadObject(r)". Didn't want to write the rest of the code for not modifying these little bits.
Instead of replacing every T by a School[], you could also change the methods to
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);
}
and call them via serializesample<School[]>(array)
and ReadObject<School[]>(r)
.
I am just not sure what exactly you did with the T's in your code and I guess the problem could be somewhere there. Hope that helps, even though it probably won't help if the exception comes from the Monotouch [Preserve(AllMembers=true)].
精彩评论