【问题标题】:Creating separate XML documents for Winforms input data in C#在 C# 中为 Winforms 输入数据创建单独的 XML 文档
【发布时间】:2017-01-12 09:11:13
【问题描述】:

我是 C# 开发的新手,目前正在为码头构建一个预订应用程序。

(已经搜索过以前的问题,但没有成功找到我正在寻找的具体内容)

用例:

  1. 通过windows窗体界面输入客户信息,
  2. 将数据存储为 XML 文件 - 单击按钮时(每位客户一个)
  3. 通过表单按名称搜索时将数据返回给 gridView。 (例如客户端查找)

我已将程序配置为为表单输入创建 XML 文件, 但是,我不知道如何为每个条目创建单独的 XML 文件。

目前,每次输入表单数据,都会覆盖文件中之前的XML数据。

非常感谢任何关于如何按照上述创建单独/附加 XML 文件的解决方案。

第一次在这里发帖,如有遗漏,敬请见谅。

代码如下:

  // Save XML.cs

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Serialization;
    using System.IO;

    namespace Popeye_Booking_application
    {
        public class SaveXml
        {
            public static void SaveData(object obj, string filename)
            {
                XmlSerializer sr = new XmlSerializer(obj.GetType());
                TextWriter writer = new StreamWriter(filename);
                sr.Serialize(writer, obj);
                writer.Close();
                }
            }
        }



// Information.cs



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Popeye_Booking_application
    {
        public class Information
        {
            private string data1;
            private string data2;
            private string data3;
            private string data4;
            private string data5;
            private string data6;
            private string data7;

            public string Data1
            {
                get  { return data1; }
                set { data1 = value; }
            }

            public string Data2
            {
                get { return data2; }
                set { data2 = value; }
            }

            public string Data3
            {
                get { return data3; }
                set { data3 = value; }
            }

            public string Data4
            {
                get { return data4; }
                set { data4 = value; }
            }

            public string Data5
            {
                get { return data5; }
                set { data5 = value; }
            }

            public string Data6
            {
                get { return data6; }
                set { data6 = value; }
            }

            public string Data7
            {
                get { return data7; }
                set { data7 = value; }
            }

        }
    }


// Form.cs

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            try
            {
                Information info = new Information();
                info.Data1 = textBoxData1.Text;
                info.Data2 = textBoxData2.Text;
                info.Data3 = textBoxData3.Text;
                info.Data4 = textBoxData4.Text;
                info.Data5 = textBoxData5.Text;
                info.Data6 = textBoxData6.Text;
                info.Data7 = textBoxData7.Text;

               SaveXml.SaveData(info, "data.xml");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

提前致谢,

【问题讨论】:

  • 如果您在每个 Information 中都有一个身份字段,只需将其用作 XML 文件的名称;)。

标签: c# xml winforms


【解决方案1】:

使用数据表。它更容易阅读和写作。还要在表单中添加一个 DataGridView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace Popeye_Booking_application
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\test.xml";
        DataTable dt = new DataTable();
        public Form1()
        {
            InitializeComponent();
            if (File.Exists(FILENAME))
            {
                dt.ReadXml(FILENAME);
                dataGridView1.DataSource = dt;
            }
            else
            {
                dt.TableName = "Data";
                dt.Columns.Add("Data1",typeof(string));
                dt.Columns.Add("Data2", typeof(string));
                dt.Columns.Add("Data3", typeof(string));
                dt.Columns.Add("Data4", typeof(string));
                dt.Columns.Add("Data5", typeof(string));
                dt.Columns.Add("Data6", typeof(string));
                dt.Columns.Add("Data7", typeof(string));
                SaveXml.SaveData(dt, FILENAME);
            }

        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            try
            {
                Information info = new Information();
                info.Data1 = textBoxData1.Text;
                info.Data2 = textBoxData2.Text;
                info.Data3 = textBoxData3.Text;
                info.Data4 = textBoxData4.Text;
                info.Data5 = textBoxData5.Text;
                info.Data6 = textBoxData6.Text;
                info.Data7 = textBoxData7.Text;

                dt.Rows.Add(new object[] {
                    info.Data1,
                    info.Data2,
                    info.Data3,
                    info.Data4,
                    info.Data5,
                    info.Data6,
                    info.Data7
                });
                dt.AcceptChanges();

                dataGridView1.DataSource = null;
                dataGridView1.DataSource = dt;
                SaveXml.SaveData(dt, FILENAME);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


    }
    public class SaveXml
    {
        public static void SaveData(DataTable dt, string filename)
        {
            dt.WriteXml(filename, XmlWriteMode.WriteSchema);
        }
    }

    public class Information
    {
        private string data1;
        private string data2;
        private string data3;
        private string data4;
        private string data5;
        private string data6;
        private string data7;

        public string Data1
        {
            get { return data1; }
            set { data1 = value; }
        }

        public string Data2
        {
            get { return data2; }
            set { data2 = value; }
        }

        public string Data3
        {
            get { return data3; }
            set { data3 = value; }
        }

        public string Data4
        {
            get { return data4; }
            set { data4 = value; }
        }

        public string Data5
        {
            get { return data5; }
            set { data5 = value; }
        }

        public string Data6
        {
            get { return data6; }
            set { data6 = value; }
        }

        public string Data7
        {
            get { return data7; }
            set { data7 = value; }
        }

    }

}

【讨论】:

  • 这对我创建员工的职责表很有帮助,非常感谢。
  • 现在我将寻找编辑/更新这些条目的方法;)
【解决方案2】:

使用 XDocument 类为每个条目创建一个对象,然后在其中使用 Save 方法,请查看此链接:https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx

【讨论】:

  • 使用XDocument 不会带来任何价值,相反您需要手动将值写入XDocument。其中XmlSerializer会根据提供的对象类型自动生成xml文件。
【解决方案3】:

但是,我不知道如何为每个条目创建单独的 XML 文件。

您需要为每个条目指定一个可区分的特定 ID 或名称。所以我建议在 Information 类中为它创建一个额外的属性:

public class Information
{

    public string Name { get; set; }

这也将帮助您管理 3. 点

3.通过表单按名称搜索时将数据返回给gridView。 (例如客户端查找)

您需要一个额外的TextBox 来输入名称:

Information info = new Information();
info.Name= textBoxName.Text;

如果它不存在,则保存它: 字符串文件名 = info.Name + ".XML"

if (!System.IO.File.Exists(filename))
{
    SaveXml.SaveData(info, filename);
}
else
{
    // notify that name is imabiguous
}

加载时您可以搜索该文件。

另一种可能性是创建一个只有 1 个属性的额外类:List<Information> InfoList。这可以与所有信息条目一起保存在一个文件中。然后可以轻松地将其绑定到DataGridView,也可以使用Name 属性轻松搜索特定项目。

编辑:

这是您要求的一个小流程:

Information 类的基本结构保持不变:

public class Information
{
    public string Name { get; set; }
    public string Data1 { get; set; }
    public string Data2 { get; set; }

    // and so on... you get the drift
}

所有这些信息都需要一个容器类。这样您就可以仅使用文件将它们全部保存;)。然后这个类也可以获取保存和加载的方法:

public class InformationSaveFile
{   // A list to safe all info entries
    public List<Information> InformationList { get; set; }

    public InformationSaveFile()
    {
        InformationList = new List<Information>();
    }

    // you method to save data
    public static void SaveData(object obj, string filename)
    {
        XmlSerializer sr = new XmlSerializer(obj.GetType());
        TextWriter writer = new StreamWriter(filename);
        sr.Serialize(writer, obj);
        writer.Close();
    }

    // a method to Load the Data
    public static InformationSaveFile LoadData(string FileName)
    {
        using (var stream = File.OpenRead(FileName))        // File ist aus dem Namensraum System.IO
        {
            var serializer = new XmlSerializer(typeof(InformationSaveFile));
            InformationSaveFile w = serializer.Deserialize(stream) as InformationSaveFile;

            return w;
        }
    }

}

在您的表单中,您需要一个容器类的实例:

// public is not necessary here
public InformationSaveFile InfoSaveFile { get; set; }

现在要添加数据,您将使用InformationList,并使用SaveData 方法保存数据,如下所示:

private void buttonCreate_Click(object sender, EventArgs e)
{
    //Adding of Info Items:
    Information info = new Information();
    info.Name = "Specific Name";
    info.Data1 = textBoxData1.Text;
    // and so on...
    InfoSaveFile.InformationList.Add(info);

    InformationSaveFile.SaveData(InfoSaveFile, "YourFileName.XML");

为了加载数据,您现在只需要相同的文件名,您添加到 InformationList 并保存的所有数据都将供您使用:

InfoSaveFile = InformationSaveFile.LoadData("YourFileName.XML");

现在您可以使用列表来过滤您需要的条目的数据:

// Search for certain names:
List<Information> infoList = InfoSaveFile.InformationList.FindAll(x => x.Name == "Search Name");
// OR looking with contains (makes the search broader)
List<Information> infoList_broad = InfoSaveFile.InformationList.FindAll(x => x.Name.Contains("Search Name"));

现在这变成了一个很长的答案。我希望你能遵循这个过程,并且答案会有所帮助。如果不只是给我评论。

【讨论】:

  • 感谢Mong Zhu 的回复,我喜欢List InfoList 的选项,但不知道如何构建它(在线教程对我不起作用!)您是否有机会简要介绍一下在这种情况下将如何构建?非常感谢。
  • @el_beeto 欢迎您。我在我的答案中添加了一个贯穿,希望它有所帮助。看看
  • 非常感谢您花时间回复@MongZhu,对于我的缓慢回复深表歉意!我会通过你的演练,看看我怎么走,亲切的问候
猜你喜欢
  • 2010-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 2023-03-28
  • 2013-09-17
  • 2019-12-02
相关资源
最近更新 更多