无法序列化对象

本文关键字:对象 序列化 | 更新日期: 2023-09-27 17:50:26

下面是我用来序列化对象的代码

College college = new College();
college= (College)(Session["XML"]);
public void serializetoxml(College college)
{
    System.Xml.Serialization.XmlSerializer myserializer = new System.Xml.Serialization.XmlSerializer(college.GetType());
    // XmlSerializer myserializer = new XmlSerializer(typeof(College));
    TextWriter mywriter = new StreamWriter("C:''invoice.xml");
    myserializer.Serialize(mywriter, college);
    mywriter.Close();
} 

对不起,我忘了粘贴我的类的代码,这里是

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="")]           [System.Xml.Serialization.XmlRootAttribute]
public partial class College{
    /// <remarks/>
    public Header header;
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Student")]
    public Student Student;
    /// <remarks/>
    public Summary summary;
}
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,Namespace="")] 
[System.Xml.Serialization.XmlRootAttribute]
public partial class Invoice {

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Activity")]
    public List<Activity> Activity;
        }
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Activity{
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]
    public string StudentNumber;

    /// <remarks/>        
    public string mark1;
    ///  <remarks/> typed it in manually
    public string mark2;
   }

这是我得到的错误{"有一个错误反映类型'A.Common.College'。"}

无法序列化对象

查看您得到的内部异常。它将告诉您序列化时遇到问题的字段/属性。

您可以通过用[XmlIgnore()]属性修饰字段/属性来排除xml序列化中的字段/属性。