Unable to serialize an object
Below is the code that I am using to serialize an object
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();
}
Sorry , I missed to paste the code for my class, here it is
[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.DebuggerStepThroughA开发者_开发百科ttribute()]
[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;
}
This is the error that I am getting {"There was an error reflecting type 'A.Common.College'."}
It is an example of realtimeclass.
Look at the inner exception that you are getting. It will tell you which field/property it is having trouble serializing.
You can exclude fields/properties from xml serialization by decorating them with the [XmlIgnore()] attribute.
精彩评论