无法序列化对象

时间:2011-04-15 00:49:08

标签: c# serialization xml-serialization

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

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'。”}

这是realtimeclass的一个例子。

1 个答案:

答案 0 :(得分:0)

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

您可以通过使用[XmlIgnore()]属性进行装饰来从xml序列化中排除字段/属性。