C# monotouch中的序列化问题

C# monotouch中的序列化问题,c#,.net,xamarin.ios,C#,.net,Xamarin.ios,我在反序列化时遇到了一个奇怪的问题,我想知道是否有人可以解释一下。抱歉,代码太粗糙了,但这只是一个原型 基本上,我正在尝试序列化和反序列化一个简单类: [Serializable] [Preserve(AllMembers=true)] public class School { public School () { } public string est_name{get; set;} public string postcod

我在反序列化时遇到了一个奇怪的问题,我想知道是否有人可以解释一下。抱歉,代码太粗糙了,但这只是一个原型

基本上,我正在尝试序列化和反序列化一个简单类:

[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> 
我在最后一行“responseObject=ReadObject(r);”中不断得到以下异常:

System.InvalidOperationException:XML文档中有错误。-->System.InvalidOperationException:(未知)不是预期的 在/Developer/MonoTouch/Source/mono/mcs/class/System.Xml/System.Xml.serializationReaderInterpreter.ReadRoot(System.Xml.Serialization.XmlTypeMapping rootMap)[0x00050]中的System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:182 在/Developer/MonoTouch/Source/mono/mcs/class/System.Xml/System.Xml.Serialization/XmlSerializationReaderInterpreter.ReadRoot()[0x00028]中的System.Xml.Serialization/XmlSerializationReaderInterpreter.cs:87 在/Developer/MonoTouch/Source/mono/mcs/class/System.Xml/System.Xml.Serialization/XmlSerializer.reader中的System.Xml.Serialization.XmlSerializer.Deserialize(System.Xml.Serialization.xmlsializationReader)[0x0001c]处 ---内部异常堆栈跟踪的结束--- 在/Developer/MonoTouch/Source/mono/mcs/class/System.Xml/System.Xml.Serialization/XmlSerializer.Deserialize(System.Xml.Serialization.XmlSerializationReader reader)[0x00061]中 在/Developer/MonoTouch/Source/mono/mcs/class/System.Xml/System.Xml.Xml.Serialization/xmlsializer.cs:350中的System.Xml.Serialization.xmlsializer.Deserialize(System.Xml.XmlReader-XmlReader)[0x0002c]处 在/Users/Khalil/Desktop/Monotouch Utility/Burnspeed.Utilities/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs:201中的Burnspeed.Utilities.WebRequestHelper
1[SchoolFinder_Prototype.School[].ReadObject(System.Xml.XmlReader stream)[0x00010]

在/Users/Khalil/Desktop/Monotouch Utility/Burnspeed.Utilities/Burnspeed.Utilities/WebRequestHelper.cs:156中的Burnspeed.Utilities.WebRequestHelper
1[SchoolFinder_Prototype.School[].ProcessHttpResponseAndFire(IAsyncResult iar)[0x000ae]中,我不完全确定为什么会发生这种异常。我尝试了你的代码,只做了一些小的修改,效果很好。我没有使用[Preserve(AllMembers=true)],我用一个School[]替换了每个t(你也这么做了,不是吗?),我用“School[]newArray=ReadObject(r)”替换了发生异常的行“e.responseObject=ReadObject(r)”,其中出现了异常。我不想写剩下的代码,因为没有修改这些小块

您也可以将方法更改为

    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);
    }
public示例(T示例){
XmlSerializer serializer=新的XmlSerializer(typeof(T));
string path=“G:\\sample.xml”;
Stream st=新文件流(路径,FileMode.OpenOrCreate);
XmlWriter w=新的XmlTextWriter(st,Encoding.UTF8);
serializer.Serialize(w,sample);
圣弗拉什();
圣克洛斯();
}
私有T ReadObject(XmlReader){
XmlSerializer serializer=新的XmlSerializer(typeof(T));
返回(T)序列化程序。反序列化(读取器);
}
并通过
serializesample(array)
ReadObject(r)
调用它们


我只是不确定你在代码中用T做了什么,我想问题可能就在那里。希望这会有所帮助,即使如果异常来自Monotouch[Preserve(AllMembers=true)],它可能不会有帮助。

我不完全确定为什么会发生这种异常。我尝试了你的代码,只做了一些小的修改,效果很好。我没有使用[Preserve(AllMembers=true)],我用一个School[]替换了每个t(你也这么做了,不是吗?),我用“School[]newArray=ReadObject(r)”替换了发生异常的行“e.responseObject=ReadObject(r)”,其中出现了异常。我不想写剩下的代码,因为没有修改这些小块

您也可以将方法更改为

    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);
    }
public示例(T示例){
XmlSerializer serializer=新的XmlSerializer(typeof(T));
string path=“G:\\sample.xml”;
Stream st=新文件流(路径,FileMode.OpenOrCreate);
XmlWriter w=新的XmlTextWriter(st,Encoding.UTF8);
serializer.Serialize(w,sample);
圣弗拉什();
圣克洛斯();
}
私有T ReadObject(XmlReader){
XmlSerializer serializer=新的XmlSerializer(typeof(T));
返回(T)序列化程序。反序列化(读取器);
}
并通过
serializesample(array)
ReadObject(r)
调用它们

我只是不确定你在代码中用T做了什么,我想问题可能就在那里。希望这会有所帮助,即使如果Monotouch[Preserve(AllMembers=true)]出现异常可能不会有帮助