C# 从 XML 文件加载日期
我有一个程序可以保存名称列表、文件路径、摘要和日期等信息。
一切都保存正常,一切从文件加载都正常...除非我从文件加载信息,不进行任何更改并重新保存文件并重新加载,第一个日期似乎以某种方式损坏了。我在编辑器中编辑了 xml 文件,一切似乎都正常。我必须更改列表中的第一个日期并在编辑器中重新保存。然后加载就ok了。
这是我仅保存日期部分的代码:
using (XmlWriter serializer = XmlWriter.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 I miss some things?
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 = XmlWriter.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 XElement 或 XmlDocument API
You can use the XElement or XmlDocument APIs