Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to XML serialize a Form in Windows Application.

I draw two buttons, and write lines as below, when running this snippet,
it shows the line of
"XmlSerializer xs = new XmlSerializer(typeof(Form));"
is error (the form type is not correct ?)

How can I correct it to let it work fine? Thanks.

C#
private void button2_Click(object sender, EventArgs e)
{
     Form SaveForm = new Form();
     SaveForm.Location = new Point(100, 100);
     FileStream fs = new FileStream("SaveForm.tmp",
                     FileMode.Create, FileAccess.Write);
     XmlSerializer xs = new XmlSerializer(typeof(Form));
     xs.Serialize(fs, SaveForm);
     fs.Close();
}

private void button3_Click(object sender, EventArgs e)
{
     Form SaveForm;
     FileStream fs = new FileStream("SaveForm.tmp", 
                     FileMode.Open, FileAccess.Read);
     XmlSerializer xs = new XmlSerializer(typeof(Form));
     SaveForm = (Form)xs.Deserialize(fs);
     fs.Close();
     SaveForm.BringToFront();
     SaveForm.Show();
}
Posted
Updated 17-Nov-10 0:31am
v3
Comments
Sandeep Mewara 17-Nov-10 6:31am    
Use PRE tags to format the code part from next time. It makes the question readable.

You can't. The Form class is not Serializable. You'll have to save off the properties you want yourself. Why are you trying to serialize a Form anyway? There's no reason for it.
 
Share this answer
 
Comments
Sports Kuo 17-Nov-10 21:17pm    
======================================= new comment
Thanks Dave of your answer.

In fact it's a long story, please see below in patient.
1. I am writing a windows application, the main form includes :
1.1 a ContentPanel. (in order to add system built-in toolstripcontainer, since it can automatically doing docking/floating/... issues)
1.2 on this ContentPanel, user will create several forms. (so I have to do "toolStripContainer1.ContentPanel.Controls.Add(form)" for all forms created by user)
1.3 on each form, user will select several objects (it's a panel with an image on it), so I have to do
"Form.Controls.Add(ControlPanel);" for all objects)
2. After creating several forms with objects, user may save all data into files, and will read it for next use,
so I have to display all forms with objects on main form as defined at last time.
3. The questions now are :
3.1 How can I save those huge data into file ?
3.2 Is there any simple serialization / deserialization way to save form objects and panel objects into file ? (so I do not need to get/set form/object related data so detailed)
3.3 Any suggestion or comments on all possible way to quick reach the purpose ?
OK, you still can't serialize a form.

You're going to implement a kind of Command pattern to keep track of the steps necessary to recreate what the user does. For example, if the user drops a panel control on the form, you create a Command object that contains the details of the type of control and its properties in a serializable collection. Then you send that command to your designer, which will actually create the panel with the proper properties and show it.
 
Share this answer
 
Comments
Sports Kuo 17-Nov-10 22:40pm    
Dave, thanks so much of your guideline.
So, I understand I have to do the command pattern to track the steps, yes, but, it's indeed a huge task.
Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900