I was unable to find anything equivalent for XML, so did the following
/// <summary>
/// overriding read xml to trim whitespace
/// </summary>
/// <seealso cref="System.Net.Http.Formatting.XmlMediaTypeFormatter" />
public class CustomXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
var task = base.ReadFromStreamAsync(type, readStream, content, formatterLogger);
// the inner workings of the above don't actually do anything async
// so not actually breaking the async by getting result here.
var result = task.Result;
if (result.GetType() == type)
{
// okay - go through each property and trim / nullify if string
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var p in properties)
{
if (p.PropertyType != typeof(string))
{
continue;
}
if (!p.CanRead || !p.CanWrite)
{
continue;
}
var value = (string)p.GetValue(result, null);
if (string.IsNullOrWhiteSpace(value))
{
p.SetValue(result, null);
}
else
{
p.SetValue(result, value.Trim());
}
}
}
return task;
}
}
and then changed the default XmlMediaTypeFormatter
to
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.Add(new CustomXmlMediaTypeFormatter());