如何在不影响现有xml文件的情况下更新新值?通过序列化,没有任何数据库

时间:2019-07-15 08:04:16

标签: c# xml serialization deserialization

嗨,我在Windows窗体中使用它,这里我使用的按钮是序列化。加载窗体时,使用refresh()方法进行反序列化。但是如果我必须更新新值,则现有文件将被删除,新值将保存在该文件中。

    private void button1_Click(object sender, EventArgs e)
    {

        {
            cl.cll[listBox1.SelectedIndex].trackbar1 = trackBar1.Value;
            cl.cll[listBox1.SelectedIndex].trackbar2 = trackBar2.Value;
            cl.cll[listBox1.SelectedIndex].textbox1 = textBox1.Text;
            cl.cll[listBox1.SelectedIndex].textbox2 = textBox2.Text;
            xs = new XmlSerializer(typeof(Class1));
            try
            {
                tw = new StreamWriter("d://" + "kml123" + ".xml");
                xs.Serialize(tw, cl);
                Update();
                tw.Close();
             }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    }

 public void Refresh()
    {
        try
        {

            xs = new XmlSerializer(typeof(Class1));
            StreamReader sr = new StreamReader("d://" + "kml123" + ".xml");
            Class1 c = (Class1)xs.Deserialize(sr);
            trackBar1.Value = c.cll[listBox1.SelectedIndex].trackbar1;
            trackBar2.Value = c.cll[listBox1.SelectedIndex].trackbar2;
            textBox1.Text = c.cll[listBox1.SelectedIndex].textbox1;
            textBox2.Text = c.cll[listBox1.SelectedIndex].textbox2;
            sr.Close();
        }
        catch { }
    }

0 个答案:

没有答案