【问题标题】:Unable to serialize an object无法序列化对象
【发布时间】:2011-04-15 00:49:07
【问题描述】:

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

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

这是一个实时类的例子。

【问题讨论】:

  • 问题是什么?你的标题说无法序列化..你的错误是什么?
  • 你能提供一些关于“无法序列化”的细节吗?如果您可以发布 College 类的代码,也会有所帮助。
  • 是的,你是对的。那是你的代码。有什么问题?你有任何错误吗?
  • 文件c:\invoice.xml是否在使用中?
  • @Janet:学习包含 complete 异常。捕获异常,显示 ex.ToString() 的结果,然后在此处发布。然后,去掉你添加的 try/catch。

标签: c# serialization xml-serialization


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2014-08-04
    • 2015-05-29
    • 2016-03-04
    • 1970-01-01
    相关资源
    最近更新 更多