How do I save a DateTime value in an XML file?

  • Thread starter Thread starter Frank Meng
  • Start date Start date
F

Frank Meng

Hi.
I want to save a DateTime value with format like
"2004-02-11T12:11:17.0000000-05:00" using XmlTextWriter.
I think I should write codes something like:

StreamWriter sw=new StreamWriter(FileName);
XmlTextWriter writer = new XmlTextWriter(sw);
// ...
writer.WriteStartElement("FileTime");
DateTime TempDateTime=ChangeInfo.FileTime;
// writer.Write??? // What should I write here?
writer.WriteEndElement(); // FileTime

Where can I find some sample codes?
Thank you for the help in advance.
Frank
 
Alfred Taylor said:
Take a look at custom DateTime format specifiers.

http://msdn.microsoft.com/library/d...ide/html/cpconcustomdatetimeformatstrings.asp

For example, if you wanted to output DateTime.Now into the format MM/DD/YYYY
you'd have something along these lines:

DateTime.Now.ToString("MM/dd/yyyy");

Don't do this... There is a specific class for doing conversions to and
from XML (to/from most native types): XmlConvert.

To save out a DateTime to an XML format, it is as simple as:

XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:ss")

Hope that helps...

John Beyer
 
ahh, i wasn't aware of this class. thanks for the heads up. it'll
definitely come in handy.
 
John Beyer said:
To save out a DateTime to an XML format, it is as simple as:

XmlConvert.ToString(DateTime.Now, "yyyy-MM-ddTHH:mm:ss")

Or even simpler:

XmlConvert.ToString(DateTime.Now);

The format you gave is the one used when you don't specify one at all.
 
Back
Top