I’ve got an XML file that is created via my Windows form to save two text fields and 2 date time pickers.
I am wondering how to “load” (preferably by asking the user where the file is) this back into my form so that it can be edited and saved again.
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;}
}
Save Button on my form
void SavebuttonClick(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to save?",
"Save", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
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);
}
}
Third Part
public void SaveValues(Values v)
{
XmlSerializer serializer = new XmlSerializer(typeof(Values));
using (TextWriter textWriter = new StreamWriter(@"E:\TheFile.xml"))
{
serializer.Serialize(textWriter, v);
}
}
You can do this:
I recommend to have the serializer in one variable so it won’t be created each time (it’s expensive to construct a new XmlSerializer)
Hope it helps