J'ai un problème étrange de désérialisation et je me demandais si quelqu'un pouvait faire la lumière là-dessus. Désolé pour le code approximatif, mais cela a été conçu comme un prototype.Problème de sérialisation dans monotouch
Fondamentalement, je tente de sérialisation et la désérialisation une classe simple:
[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;
}
Le code de sérialisation est la suivante, en utilisant l'école [] comme T, Son une méthode d'une classe générique:
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();
}
résultant dans le fichier XML suivant:
<?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>
le code désérialisation est un s suit, avec l'école [] comme 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);
Je continue à obtenir l'exception suivante à la dernière ligne cotée "responseObject = ReadObject (r);":
System.InvalidOperationException: Il y a une erreur dans un document XML. ---> System.InvalidOperationException: (inconnu) n'était pas prévu à System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x00050] dans/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializationReaderInterpreter.cs: 182 à System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot() [0x00028] dans /Developer/MonoTouch/Source/mono/mcs/class/System.XML /System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87 à System.Xml.Serialization.XmlSerializer.Deserialize (lecteur System.Xml.Serialization.XmlSerializationReader) [0x0001c] dans/Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs: 361 --- Fin de la trace de pile d'exception interne --- at System.Xml.Serialization.XmlSerializer.Deser ialize (lecteur System.Xml.Serialization.XmlSerializationReader) [0x00061] dans /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] dans /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml.Serialization/XmlSerializer.cs:350 à 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 []] ProcessHttpResponseAndFire (IAsyncResult IAR) [0x000ae] dans/Users/Khalil/Bureau/MonoTouch Utilitaire/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs. 156