C# Loading dates from XML file
I have a program that is saves information like a list of names, file paths, summary, and dates.
Everything saves ok and everything load ok from the file... except if I load the information in from the file, don't make any changes and re-save the file again and reload, the first date seems to get corrupted somehow. I edit the xml file in an editor and it all seems to be ok. I have to alter the first date on the list and re-save in the editor. Then it will load ok.
Here is my save code for the date portion only:
using (XmlWriter serializer = X开发者_开发技巧mlWriter.Create(saveFileDialog1.FileName))
{
serializer.Flush();
serializer.WriteStartDocument();
serializer.WriteStartElement("Items");
serializer.WriteElementString("DRNumber", TextBox1.Text.ToString());
serializer.WriteElementString("PicsPath", textBox3.Text.ToString());
serializer.WriteElementString("DocPath", textBox4.Text.ToString());
foreach (DateTime fname in date)
{
serializer.WriteElementString("Date", fname.ToShortDateString());
}
serializer.WriteEndElement();
serializer.WriteEndDocument();
serializer.Flush();
serializer.Close();
FileInfo fi1 = new FileInfo(saveFileDialog1.FileName);
}
Is this the best way to write dates to an XML, or am I missing something?
You can use the XElement or XmlDocument APIs
XElement root = new XElement("Root",new XElement("Child", "child content"));
root.Save("Root.xml");
精彩评论