简体   繁体   English

如何将 GUI 中的信息保存到 XML 文件中?

[英]How do I save information from an GUI into a XML-file?

I've created a program where users can input a few information and then can save everything into an xml file, or open it.我创建了一个程序,用户可以在其中输入一些信息,然后可以将所有内容保存到 xml 文件中,或者打开它。

Before I setup that save and open thing on the bigger scale, I created a small test-run to find a solution.在我设置更大规模的保存和打开内容之前,我创建了一个小型测试运行来寻找解决方案。 There are three textboxes where you can input your own information, two checkboxes and comboboxes where users can choose from a few options.有三个文本框,您可以在其中输入自己的信息,两个复选框和组合框,用户可以从几个选项中进行选择。 I've created a open and save menu-strip-button but can't think of a way how to save all those information into a xml.file.我创建了一个打开和保存菜单条按钮,但想不出如何将所有这些信息保存到 xml.file 中。

using System;
using System.IO;
using System.Windows.Forms;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void openToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            var fileContent = string.Empty;
            var filePath = string.Empty;

            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "XML-File | *.xml|All files (*.*)|*.*";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    //Get the path of specified file
                    filePath = openFileDialog.FileName;

                    //Read the contents of the file into a stream
                    var fileStream = openFileDialog.OpenFile();

                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                        fileContent = reader.ReadToEnd();
                    }
                }
            }

            //MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "XML-File | *.xml|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                }
            }
        }

        /*private void validateUserEntry()
        {
            // Checks the value of the text.
            if (textBox1.Text.Length == 0 && textBox2.Text.Length == 0 && textBox3.Text.Length == 0)
            {
                // Initializes the variables to pass to the MessageBox.Show method.
                string message = " AAAAAAAAAAAAA";
                string caption = "Error Detected in Input";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult result;

                // Displays the MessageBox.
                result = MessageBox.Show(message, caption, buttons);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    // Closes the parent form.
                    this.Close();
                }
            }
        }*/

    }
}

ioCode.Serialization ioCode.序列化

C# types serialization library. C# 类型序列化库。
Serializes and deserializes objects into and from XML documents.在 XML 文档中序列化和反序列化对象。


    PM> Install-Package ioCode.Serialization -Version 1.0.0

Classes课程

ioCode.Serialization library has two serialization classes ioCode.Serialization 库有两个序列化类

  • XmlSerializer<T> Xml serialization object to serialize custom types XmlSerializer<T> Xml 序列化 object 序列化自定义类型
  • BinSerializer<T> Bin serialization object for XML-based serialization of custom types. BinSerializer<T> Bin 序列化 object 用于自定义类型的基于 XML 的序列化。 The serialized output file is encrypted.序列化的 output 文件被加密。

XmlSerializer<T> XmlSerializer<T>

Write to file写入文件

  Product product = new Product();
  product.Name = "Product1";
  bool isSuccess = XmlSerializer<Product>.WriteFile(@"C:\users\[user]\documents\product.xml", product);
  MessageBox.Show(isSuccess ? "Success" : "Fail");

Read from file从文件中读取

  Product product = XmlSerializer<Product>.ReadFile(@"C:\users\[user]\documents\product.xml");
  MessageBox.Show((product != null) ? "Success" : "Fail");

BinSerializer<T> BinSerializer<T>

Write to file写入文件

  Product product = new Product();
  product.Name = "Product1";
  bool isSuccess = BinSerializer<Product>.WriteFile(@"C:\users\[user]\documents\product.bin", "password123", product);
  MessageBox.Show(isSuccess ? "Success" : "Fail");

Read from file从文件中读取

  Product product = BinSerializer<Product>.ReadFile(@"C:\users\[user]\documents\product.bin", "password123");
  MessageBox.Show((product != null) ? "Success" : "Fail");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM