Web Api 2 DatetimeOffset XML Serialization Issue

Viewed 2484

Below is the property in my model:

 public DateTimeOffset AcquireDate { get; set; }

Below is the configuration in WebApiConfig:

config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.Add(new XmlMediaTypeFormatter()); 

Below is the json response (date is in IS 8601 format):

enter image description here

Below is the XML response:

<AcquireDate xmlns:d3p1="http://schemas.datacontract.org/2004/07/System">
      <d3p1:DateTime>2008-01-10T16:40:12.1523923Z</d3p1:DateTime>
      <d3p1:OffsetMinutes>330</d3p1:OffsetMinutes>
</AcquireDate>

From fiddler:

enter image description here

In Xml response, datetime and offset are available in two different elements. I want DateTimeOffset as single value, just like json response (in ISO 8601 format).

I could use one more property which would be of type string and that way my problem could be solved (empty setter requires for this property to be serialized).

[DataMember(Name="AcquireDate")]
    public string AcquireDateString
    {
        get
        {
            return AcquireDate.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
        }
        set {
            AcquireDate = DateTimeOffset.Parse(value); 
        }
    }

Any other solution apart from that?

Solution should not affect existing Json Serialization which is working fine.

1 Answers
Related