Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serialization problem, "there was an error reflecting..."

Status
Not open for further replies.

marcarls

Programmer
Feb 7, 2002
40
SE
Dear friends,
I am trying a simple serialization example.
I have a class Employee having few fields and Methods
I have a another class as Employees that implements the ICollection interface.
In windows form code module i have two object variable's of employee class that I add to the Employees class collection object. Further I am trying to serialize the employees collection as file. This gives me the error "There was an error reflecting 'MyProject.Employees"
Any suggestions or hit what I am doing wrong?
Thanks
 
XMLSerializer will only serialize public methods, and will not serialize any non-common type system variables (you can't use it to serialize a TimeSpan object, for example, but works fine for strings, doubles, ints, etc). It also has the limitation that it won't serialize all the different types of collection objects -- only certain ones.

The BinaryFormatter does serialize private member variables, but it has the restriction that the version number of the assembly doing the deserialization must match the version number of the assembly that did the serialization earlier. It also does not serialize nested objects, unless you implement the ISerializable interface (custom serialization).

Because of these limitations I've always written my own serialization properties. It's a pain, but it works every time.
Code:
public string SerializeXML
{
	get
	{
		StringBuilder sb = new StringBuilder();
		StringWriter sw = new StringWriter(sb);
		XmlTextWriter tw = new XmlTextWriter(sw);

		tw.WriteStartDocument(true);
		tw.WriteStartElement(this.GetType().Name);

		tw.WriteElementString("CustomerID", XmlConvert.ToString(_customerID));
		tw.WriteElementString("Code", _code);
		tw.WriteElementString("Name", _name);
		tw.WriteElementString("Location", _location);
		tw.WriteElementString("Description", _description);
		tw.WriteElementString("CreatedOnUTC", XmlConvert.ToString(_createdOnUTC));

		tw.WriteEndElement();
		tw.WriteEndDocument();
		tw.Close();

		return sb.ToString();
	}
	set 
	{
		StringReader sr = new StringReader(value);
		XmlTextReader tr = new XmlTextReader(sr);

		tr.MoveToContent();
		tr.Read();

		_customerID = XmlConvert.ToInt32(tr.ReadElementString("CustomerID"));
		_code = tr.ReadElementString("Code");
		_name = tr.ReadElementString("Name");
		_location = tr.ReadElementString("Location");
		_description = tr.ReadElementString("Description");
		_createdOnUTC = XmlConvert.ToDateTime(tr.ReadElementString("CreatedOnUTC"));

		tr.Close();
	}
}
Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Ooops. Sorry about posting C# code in the VB.NET forum. Hopefully you get the idea.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top