How can I control DateTime
output format when content is requested as application/xml
? The following works for JSON:
JsonMediaTypeFormatter jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings = new JsonSerializerSettings {
DateFormatString = "yyyy-MM-dd" };
What's the equivalent for XmlMediaTypeFormatter
?
Update: Semantically, my data have no time information. Technically, I want to a) minimize payload b) simplify consumption (no need to process/format on client side) c) consistent responses regardless of format asked.
You shouldn't specify your own format - you should instead tell the XML serializer that the value is just meant to be a date, using:
[XmlElement(DataType="date")]
Then force the use of XML serializer with config.Formatters.XmlFormatter.UseXmlSerializer = true
and you should be fine. That way you'll be using the standard (ISO-8601) format for dates, which is yyyy-MM-dd
... which means standard tools (e.g. LINQ to XML) will be able to parse the XML appropriately.
See more on this question at Stackoverflow