如何对表格进行XML序列化? [英] How to XML serialization a Form?

查看:81
本文介绍了如何对表格进行XML序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对Windows应用程序中的表单进行XML序列化.

运行此代码段时,我绘制了两个按钮,并按如下所示编写了行,
它显示了
的行 "XmlSerializer xs =新的XmlSerializer(typeof(Form));"
错误(表单类型不正确?)

我如何纠正它以使其正常工作?谢谢.

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.

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();
}

推荐答案

您不能. Form类不可序列化.您必须保存想要的属性.您为什么仍要尝试序列化表格?没有理由.
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.


好,您仍然无法序列化表单.

您将实现一种Command模式,以跟踪重新创建用户操作所必需的步骤.例如,如果用户在面板上放置面板控件,则创建一个Command对象,该对象在可序列化集合中包含控件类型及其属性的详细信息.然后,您将该命令发送给设计人员,设计人员将实际创建具有适当属性的面板并将其显示.
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.


这篇关于如何对表格进行XML序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆