Windows 窗体 XML 序列化保存对话框

本文关键字:保存 对话框 序列化 XML 窗体 Windows | 更新日期: 2023-09-27 18:32:57

我坚持这种愚蠢的形式......这是我的代码。它将其保存在流编写器想要的地方,但是当我通过保存对话框将其保存在用户想要的位置时,它会创建XML,但没有在其中放置任何东西!有人可以看看它开始缠绕我吗!

void SavebuttonClick(object sender, EventArgs e)
{
    Stream myStream ;
    SaveFileDialog savefile1 = new SaveFileDialog();
    savefile1.Filter = "xml files |*.xml"  ;
    savefile1.FilterIndex = 2 ;
    savefile1.RestoreDirectory = true ;
    if(savefile1.ShowDialog() == DialogResult.OK)
    {
        if((myStream = savefile1.OpenFile()) != null)
        {
            Values v = new Values();
            v.task1_name = this.task1_name.Text;
            v.task1_desc = this.task1_desc.Text;
            v.task1_date = this.task1_date.Value;
            v.task1_time = this.task1_time.Value;
            SaveValues(v);
        }
        myStream.Close();   
    }
}

这是流写员...

public void SaveValues(Values v)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Values));
    using(TextWriter textWriter = new StreamWriter(@"E:'TheFileYouWantToStore.xml"))
    {
        serializer.Serialize(textWriter, v);
    }
...
}

编辑:

public class Values 
{
public string task1_name { get; set;}
public string task1_desc { get; set;}
public DateTime task1_date { get; set;}
public DateTime task1_time { get; set;}
}

我想这就是你的意思,虽然我:(编码,但我对编码相当陌生

Windows 窗体 XML 序列化保存对话框

序列化

后必须调用textWriter.close();。如果您不关闭编写器,则不会将 chenges 应用于文件。顺便说一下,您将值写入 E:''TheFileYouWantToStore.xml。保存保存方法不使用用户文件。

public void SaveValues(Values v)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Values));
    using(TextWriter textWriter = new StreamWriter(@"E:'TheFileYouWantToStore.xml"))
    {
        serializer.Serialize(textWriter, v);
        textWriter.close();
    }
...
}

编辑:

if(savefile1.ShowDialog() == DialogResult.OK)
{
        Values v = new Values();
        v.task1_name = this.task1_name.Text;
        v.task1_desc = this.task1_desc.Text;
        v.task1_date = this.task1_date.Value;
        v.task1_time = this.task1_time.Value;
        SaveValues(savefile1.FileName, v);
}

-

public void SaveValues(string fileName, Values v)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Values));
    using(TextWriter textWriter = new StreamWriter(fileName))
    {
        serializer.Serialize(textWriter, v);
        textWriter.close();
    }
...
}