Windows Form Save to XML(Windows 窗体保存到 XML)
问题描述
我有一个表单,其中包含用户输入的信息,我想将其保存到 XML...我对编程相当陌生,但阅读过 XML 是最好的选择.我该怎么办?如果它有助于我使用 Sharp Develop 作为 IDE.目前它有 10 个文本框和 10 个日期时间选择器.
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-序列化
更多细节:
这是超级基本的面向对象/Windows 窗体的东西.
This is super basic Object Oriented/Windows Forms stuff.
创建一个存储每个值的类:
Create a Class that stores each of the values:
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 方法来序列化 xml" rel="nofollow">XmlSerializer 用于序列化和 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Windows 窗体保存到 XML


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01