在 C# .net 中序列化文本框
我是.NET环境的初学者。 我有一个带有三个文本框和一个按钮的 Windows 应用程序。当用户单击按钮时,我希望所有文本框值以 XML 格式序列化到文件中。 我尝试这样做,
DialogResult dr = new DialogResult();
private void button1_Click(object sender, EventArgs e)
{
AddCustomer customer = new AddCustomer();
customer.textBox1.Text = textBox1.Text;
customer.textBox2.Text = textBox2.Text;
customer.textBox3.Text = textBox3.Text;
customer.textBox4.Text = textBox4.Text;
saveFileDialog1.InitialDirectory = @"D:";
saveFileDialog1.Filter = "Xml Files | *.xml";
if (saveFileDialog1.ShowDialog().Equals(DialogResult.OK))
{
SerializeToXML(customer);
}
}
public void SerializeToXML(AddCustomer customer)
{
XmlSerializer serializer = new XmlSerializer(typeof(AddCustomer));
TextWriter textWriter = new StreamWriter(@"D:\customer.xml");
serializer.Serialize(textWriter, customer);
textWriter.Close();
}
这返回了 system.invalidoperationexception 是未处理的异常
有什么想法吗? 谢谢, 迈克尔
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法序列化控件,而是必须创建表示 TextBox 值的
Serialized
组件。 (有关更多详细信息,请阅读 MSDN 文章)。例如,
You can't serialize controls instead you have to create
Serializable
component that represent TextBox values. (For more detail read MSDN article).For instance,
您不应该序列化文本框对象,而只能序列化它们的值。
customer.textBox1
应该是字符串类型的customer.text1
。然后,您只需分配customer.text1 = textbox1.text
。另外,使用
[Serialized]
属性标记您的AddCustomer
类。编辑:这是我刚刚尝试过的代码,它工作正常。看看您是否可以使其在您的解决方案中发挥作用。
添加新类
Customer
尝试像这样序列化它
完成此操作后,我得到了一个使用以下内容创建的 xml 文件。
尝试比较一下,看看自己做错了什么。
You shouldn't serialize the textbox object, only their values.
customer.textBox1
should becustomer.text1
of type string. You then need to just assigncustomer.text1 = textbox1.text
.Also, mark your
AddCustomer
class with the[Serializable]
attribute.Edit: This is a code I just tried and it works fine. See if you can make it work in your solution.
Add new class
Customer
Try to serialize it like this
After doing this, I got an xml file created with the following content.
Try and compare to see what you are doing wrong.
在 .NET 中编写 XML 的方法有很多种。这是使用
XmlWriter
适用于非常简单的内容,例如本例:一个注意事项:您应该避免在 UI 线程上执行文件操作,因为这可能会导致阻塞行为(例如,磁盘可能会很慢并导致 UI 在写入时冻结)文件,或者它可能正在写入网络位置并在连接时挂起一段时间)。最好弹出一个进度对话框,并显示一条消息“文件正在保存中,请稍候...”,并在后台进行文件操作;一个简单的方法是使用
BeginInvoke将后台操作发布到线程池/<代码>EndInvoke。
如果您想改用 XmlSerializer,则可以按照以下步骤操作:
public
类型作为文档的根元素,并用XmlRoot
。public
自定义类型组成的元素/属性,这些类型也是 XML 可序列化的,并用XmlElement
或XmlAttribute
(根据需要)。XmlSerializer使用适当的
。Stream
、StreamWriter
或XmlWriter
以及根对象来序列化XmlSerializer.Deseralize
使用适当的Stream
、TextReader
或XmlReader
,转换返回值输入回您的根对象。完整示例。
要序列化的类型:
读取/写入数据的代码:
There are a lot of ways to write XML in .NET. Here is a way using
XmlWriter
that works for very simple content like in this case:One note: you should avoid doing file operations on the UI thread as this can result in blocking behavior (e.g. the disk can be slow and cause the UI to freeze up while it writes the file, or it could be writing to a network location and hang for a while as it connects). It is best to put up a progress dialog and display a message "Please wait while file is saved..." and do the file operation in the background; a simple way is to post the background operation the thread pool using
BeginInvoke
/EndInvoke
.If you want to use the XmlSerializer instead, then you would follow these steps:
public
type to act as the root element of your document and mark it withXmlRoot
.public
custom types which are also XML serializable, marking them withXmlElement
orXmlAttribute
as necessary.XmlSerializer.Serialize
with an appropriateStream
,StreamWriter
, orXmlWriter
along with your root object.XmlSerializer.Deseralize
with an appropriateStream
,TextReader
, orXmlReader
, casting the return type back to your root object.Full sample.
The type to serialize:
The code to read/write the data: