Skip to main content
gamedev.net
🔒 Locked

[.net] XmlSerializer with Windows::Forms

Started by billy_zelsnack 4 replies 5.4k views
Original Post
billy_zelsnack
billy_zelsnack
If I run XmlSerializer on a simple class it works fine, but if I inherit that simple class from Forms I get a lovely not-too-helpful error at runtime.. ------------- An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There was an error reflecting type 'Lame'. ------------- FileNotFoundException?? wtf? Below is the code. If inheriting Form is commented out it works. Uncomment.. No worky. Please tell me I am doing something dumb. btw. I'm using the express vc++ (if that matters.) -------------

public ref class Lame //: public Form
{

public:

	String^ _cheese;
	
public:

	Lame() 
	{
		_cheese="5";
	}

};


void serialize_test()
{
	FileStream^ stream=File::Create("c:/lame.xml");

	Lame^ lame=gcnew Lame();

	XmlSerializer^ serializer=gcnew XmlSerializer(lame->GetType());
	serializer->Serialize(stream,lame);

	stream->Close();
}
very unhelpful and/or unfriendly
mutex
mutex
Here's a decent article on troubleshooting XML serialization problems that seems relevant given the errors you're seeing.

I'm not sure you can serialize entire forms; maybe it'd be better to construct another object to serialize from your form.

billy_zelsnack
billy_zelsnack
Thanks for the fast reply. This'll help. I didn't find it in my search.

btw. InnerException.. wtf!!
very unhelpful and/or unfriendly
turnpast
turnpast
The root of your problem is probably, as mutex suggested, that Form (I am assuming System.Windows.Forms.Form) is not serializable -- the runtime is supposed to throw a SerializationException if you try to serialize somehting like a Form, but your sub-classing may be messing with that.

So why is Form not serializable? Probably because it is an object that maintains references to system resources, like window handles, that cannot be effectively serialized or deserialized (esp. deserialized). If you need to save the configuration of a form you will most likely have to extract the parts of interest (Size, Poistion, Layout etc.) and serialize those seperately as well as recreating and configuring the form when deserializing.

Think about what should happen when you seraliaze and deserialize a Form? Should the window re-appear on the screen as soon as you deserialize it? Should the serialized object be modified to some state that is inconsistant with the state it was serialized from? What if the system you are deserializing on does not have a monitor? What if you deserialize it into the same running application: should it make an identical window with a different handle or revert the existing window to whatever the serialized state is?

When you look through the docs you will notice that some classes have the SerializableAttribute and some do not. Those that havit can be serialized and those that do not cannot. Note that the SerializableAttribute is not inherited.

Serialization is really for data. When you try to mix it with function and with system resources you are going to have a bad time. It is novel to think that you could save the entire state of your running program, but that is rarely the reality.

Quote:
btw. InnerException.. wtf!!
Sometimes exceptions caue other exceptions. This is how you find out when that is the case.
billy_zelsnack
billy_zelsnack
It just seems like a pretty reasonable thing to be able to do..

Hey, I have this gui, now I want to persist it.
very unhelpful and/or unfriendly
turnpast
turnpast
Quote:
Original post by billy_zelsnack
It just seems like a pretty reasonable thing to be able to do..

Hey, I have this gui, now I want to persist it.


Sure, it is reasonable. Reasonable to want to do anyway. It is also well outside the scope and beyond the capibilities of serialization alone.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.