Windows窗体保存为XML [英] Windows Form Save to XML
问题描述
我有在它的信息的形式,用户输入,我想这个保存到XML ...我是相当新的节目,但已经阅读XML是使用最好的事情。我将如何做呢?如果使用IM帮助夏普发展成为一个IDE。目前它有10个文本框和10 datetimepickers。
I have a form with information in it that the user enters, i want to save this to XML... i'm fairly new to programming but have read XML is the best thing to use. How would i go about it? If it helps im using Sharp Develop as an IDE. Current it has 10 text boxes and 10 datetimepickers.
推荐答案
最简单的事情是创建一个存储的10个值作为一类,性能和使用XML序列化将其转换为XML,然后将其存储到文件系统
The easiest thing would be to create a class that stores those 10 values as properties and use xml serialization to convert it to XML, then store it to the file system.
下面是一个教程:的 http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
详细资料:
这是面向/ Windows的超级基本画线形成的东西。
This is super basic Object Oriented/Windows Forms stuff.
创建存储每个值的类:
public class Values{
public string YourFirstValue { get; set;}
public DateTime YourSecondValue { get; set;}
...
}
当然你会。希望映射到他们的实际意义,但这些应该足够了现在的名字。
and of course you'd want names that map to their actual meanings, but these should suffice for now.
然后,单击窗体上按钮时,存储在类中的值:
Then, when clicking a button on your form, store the values in that class:
void Button1_OnClick(object sender, EventArgs args){
Values v = new Values();
v.YourFirstValue = this.FirstField.Text;
v.YourSecondValue = this.YourSecondField.Value
...
SaveValues(v);
}
然后实施 SaveValues
方法序列化使用 XmlSerializer的为XML序列化和的StreamWriter 将结果保存到一个文件
Then implement the SaveValues
method to serialize the xml using XmlSerializer for the serialization and StreamWriter to store the result to a file.
public void SaveValues(Values v){
XmlSerializer serializer = new XmlSerializer(typeof(Values));
using(TextWriter textWriter = new StreamWriter(@"C:\TheFileYouWantToStore.xml")){
serializer.Serialize(textWriter, movie);
}
}
这篇关于Windows窗体保存为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!