Hi,
I'm getting an odd result when serializing a DateTime field using XmlSerializer.
I have the following class:
public class RecordExample
{
[XmlElement("TheTime", DataType = "time")]
public DateTime TheTime { get; set; }
[XmlElement("TheDate", DataType = "date")]
public DateTime TheDate { get; set; }
public static bool Serialize(Stream stream, object obj, Type objType, Encoding encoding)
{
try
{
using (var writer = XmlWriter.Create(stream, new XmlWriterSettings { Encoding = encoding }))
{
var xmlSerializer = new XmlSerializer(objType);
if (writer != null) xmlSerializer.Serialize(writer, obj);
}
return true;
}
catch (Exception)
{
return false;
}
}
}
When i call the use the XmlSerializer with the following testing code:
var obj = new RecordExample {TheDate = DateTime.Now.Date, TheTime = new DateTime(0001, 1, 1, 12, 00, 00)};
var ms = new MemoryStream();
RecordExample.Serialize(ms, obj, typeof (RecordExample), Encoding.UTF8);
txtSource2.Text = Encoding.UTF8.GetString(ms.ToArray());
I get some strange results, here's the xml that is produced:
<?xml version="1.0" encoding="utf-8"?>
<RecordExample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TheTime>12:00:00.0000000+00:00</TheTime>
<TheDate>2010-03-08</TheDate>
</RecordExample>
Any idea's how i can get the "TheTime" element to contain a time which looks more like this:
<TheTime>12:00:00.0Z</TheTime>
...as that's what i was expecting?
Thanks
Dave