1

我坚持这种愚蠢的形式......继承人我的代码。它将它保存在流编写器想要的位置,但是当我通过保存对话框将它保存在用户想要的位置时,它会创建 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;}
}

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

4

1 回答 1

0

您必须textWriter.close();在序列化后调用。如果您不关闭 writer,则不会将 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();
    }
...
}
于 2012-05-03T12:19:14.253 回答