【发布时间】: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