C# 如何在“保存”按钮中编码?

C# 如何在“保存”按钮中编码?,c#,C#,我使用了五个文本框,我想用C#的save按钮将文本框中的所有内容保存到一个文件中 我将另存为xml以使其更易于阅读。使用数据集可以轻松地读取和写入xml using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Wind

我使用了五个文本框,我想用C#的save按钮将文本框中的所有内容保存到一个文件中


我将另存为xml以使其更易于阅读。使用数据集可以轻松地读取和写入xml

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\text.xml";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("Data Set");

            DataTable dt = new DataTable("Data");
            ds.Tables.Add(dt);

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Value", typeof(string));

            dt.Rows.Add(new object[] { "Text Box 1", textBox1.Text });
            dt.Rows.Add(new object[] { "Text Box 2", textBox2.Text });
            dt.Rows.Add(new object[] { "Text Box 3", textBox3.Text });
            dt.Rows.Add(new object[] { "Text Box 4", textBox4.Text });

            ds.WriteXml(FILENAME, XmlWriteMode.WriteSchema);

            //use following to read
            ds.ReadXml(FILENAME);
            textBox1.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 1").Select(x => x.Field<string>("Value")).FirstOrDefault();
            textBox2.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 2").Select(x => x.Field<string>("Value")).FirstOrDefault();
            textBox3.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 3").Select(x => x.Field<string>("Value")).FirstOrDefault();
            textBox4.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 4").Select(x => x.Field<string>("Value")).FirstOrDefault();
        }


    }
}
​
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
常量字符串文件名=@“c:\temp\text.xml”;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
数据集ds=新数据集(“数据集”);
DataTable dt=新的DataTable(“数据”);
ds.Tables.Add(dt);
添加(“名称”,类型(字符串));
添加(“值”,类型(字符串));
添加(新对象[]{“文本框1”,textBox1.Text});
添加(新对象[]{“文本框2”,textBox2.Text});
添加(新对象[]{“文本框3”,textBox3.Text});
添加(新对象[]{“文本框4”,textBox4.Text});
ds.WriteXml(文件名,XmlWriteMode.WriteSchema);
//使用以下内容阅读
ReadXml(文件名);
textBox1.Text=ds.Tables[“Data”].AsEnumerable()。其中(x=>x.Field(“Name”)==“Text Box 1”)。选择(x=>x.Field(“Value”)).FirstOrDefault();
textBox2.Text=ds.Tables[“Data”].AsEnumerable()。其中(x=>x.Field(“Name”)==“Text Box 2”)。选择(x=>x.Field(“Value”)).FirstOrDefault();
textBox3.Text=ds.Tables[“Data”].AsEnumerable()。其中(x=>x.Field(“Name”)==“Text Box 3”)。选择(x=>x.Field(“Value”)).FirstOrDefault();
textBox4.Text=ds.Tables[“Data”].AsEnumerable()。其中(x=>x.Field(“Name”)==“Text Box 4”)。选择(x=>x.Field(“Value”)).FirstOrDefault();
}
}
}
​

您尝试了什么?为什么需要所有文本更改事件?添加文本(字符串输出=textBox1.text+“\r\n”+…,或者使用environment.newline,然后执行一个File.writealText()!(包括System.IO!)究竟是什么让您认为XML是a)易于阅读还是b)对文本框的内容有用/合适???OP需要1或2行代码。只有一条指令使用dataset方法读取xml文件。解析是另一个问题。读取xml文件比读取纯文本文件更健壮。这完全取决于上下文。我们没有。我看到的是一个新手,他可能只需要学习System.IO.File方法。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\text.xml";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("Data Set");

            DataTable dt = new DataTable("Data");
            ds.Tables.Add(dt);

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Value", typeof(string));

            dt.Rows.Add(new object[] { "Text Box 1", textBox1.Text });
            dt.Rows.Add(new object[] { "Text Box 2", textBox2.Text });
            dt.Rows.Add(new object[] { "Text Box 3", textBox3.Text });
            dt.Rows.Add(new object[] { "Text Box 4", textBox4.Text });

            ds.WriteXml(FILENAME, XmlWriteMode.WriteSchema);

            //use following to read
            ds.ReadXml(FILENAME);
            textBox1.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 1").Select(x => x.Field<string>("Value")).FirstOrDefault();
            textBox2.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 2").Select(x => x.Field<string>("Value")).FirstOrDefault();
            textBox3.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 3").Select(x => x.Field<string>("Value")).FirstOrDefault();
            textBox4.Text = ds.Tables["Data"].AsEnumerable().Where(x => x.Field<string>("Name") == "Text Box 4").Select(x => x.Field<string>("Value")).FirstOrDefault();
        }


    }
}
​