Force XmlSerializer serialize DateTime as "YYYY-MM-DD hh: mm: ss"

I have an XSD scheme for some RESTful service. When used in conjunction with the xsd.exe tool to generate C # code, xs:date generates the following code:

 [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="date")] public System.DateTime time { get { return this.timeField; } set { this.timeField = value; } } 

When deserializing XML objects using the XmlSerializer everything looks good. The problem I am facing is that the service expects dates to be formatted as YYYY-MM-DD hh:mm:ss , and the generated XSD code will apparently only produce YYYY-MM-DD .

If I manually modify XSD to type xs:dateTime , the generated C # code creates: 2010-08-20T20:07:03.915039Z .

Basically, how to make serialization produce YYYY-MM-DD hh:mm:ss ? Is there something to do with XSD or is there something I can do to modify the generated C # code?

+49
c # xml-serialization
Aug 20 '10 at 20:12
source share
4 answers

In the past, I used the following to control datetime serialization:

  • Ignore the DateTime property.
  • Create a dummy string property that serializes / deserializes the way I want

Here is an example:

 public class SomeClass { [XmlIgnore] public DateTime SomeDate { get; set; } [XmlElement("SomeDate")] public string SomeDateString { get { return this.SomeDate.ToString("yyyy-MM-dd HH:mm:ss"); } set { this.SomeDate = DateTime.Parse(value); } } } 
+96
Aug 20 '10 at 20:26
source share

Use the [XmlElement(DataType = "date")] attribute to format the value of the DateTime property as you need.

From MSDN :

Note:
An attribute that annotates a publishdate field has a DataType property. There is no type in the .NET Framework that matches the type xs: date completely. The closest match is System.DateTime, which stores date and time data. Specifying the DataType property as "date" ensures that the XmlSerializer will serialize the date part of the DateTime object.

+40
Jun 06 '12 at 8:01
source share

I believe the IXmlSerializable interface will do the trick. Then you can control how you serialize and deserialize your object.

+3
Aug 20 2018-10-21
source share

If you only need to clear the millisecond. See also:

How to trim milliseconds with .NET DateTime

And basically do something like:

  startDateTimeToUse = startDateTimeToUse.AddTicks(-(startDateTimeToUse.Ticks % TimeSpan.TicksPerSecond)); endDate = endDate.AddTicks(-(endDate.Ticks % TimeSpan.TicksPerSecond)); 

I can confirm that this is serializing:

  <startDate>2015-10-31T12:13:04</startDate> <endDate>2016-11-10T12:13:06</endDate> 

I should also point out that before I clear the milliseconds, I do this:

  var startDateTimeToUse = ssStartDateTime.ToUniversalTime(); var endDate = DateTime.Now.ToUniversalTime(); startDateTimeToUse = DateTime.SpecifyKind(startDateTimeToUse, DateTimeKind.Unspecified); endDate = DateTime.SpecifyKind(endDate, DateTimeKind.Unspecified); 

What I don’t know if this affects serialization or not at the moment

+1
Nov 10 '16 at 12:26
source share



All Articles