title: HttpError with custom payload in WebApi
body: <p>I'm trying to figure out a way to return custom payload with non 200 return code using ASP.NET WebApi. Basically I want to have </p>
  
  <pre><code>{"status":{"code":"40001", "message": "Field XXX can not be empty"}}
  </code></pre>
  
  <p>and</p>
  
  <pre><code>&lt;status&gt;&lt;code&gt;40001&lt;/code&gt;&lt;message&gt;Field XXX can not be empty&lt;/message&gt;&lt;/status&gt;
  </code></pre>
  
  <p>I'm doing it using <code>HttpError</code> class, by adding <code>Dictionary</code> with rerquired payload. It works perfectly for JSON, but fails for XML, and as I understand it boils down to <code>XmlSerializer</code> and <code>HttpError</code> not playing nicely.</p>
  
  <p>Here is a short code snippet to reproduce exactly same error:</p>
  
  <pre><code>var error = new HttpError();
  var status = new Dictionary&lt;string, string&gt;
  {
      {"code", "40001"},
      {"text", "Field XXX can not be empty"}
  };
  error.Add("status", status);
  var ser = new XmlSerializer(error.GetType());
  StringWriter strWriter = new StringWriter();
  ser.Serialize(strWriter, error);
  </code></pre>
  
  <p>In this case I'm getting <code>Xml type 'xdt:untypedAtomic' does not support a conversion from Clr type 'KeyValuePair 2' to Clr type 'String'</code></p>
  
  <p>Next step is to realize that <code>KeyValuePair</code> is not serializeable, and create separate class:</p>
  
  <pre><code>[XmlType(TypeName="Status")]
  public class ResponseStatus
  {
    public ResponseStatus(){}
    public String Code 
    { get; set; }
  
    public String Text
    { get; set; }
  }
  </code></pre>
  
  <p>which by itself serializes just fine, but throws pretty same exception if used inside HttpError:
  <code>Xml type 'List of xdt:untypedAtomic' does not support a conversion from Clr type 'ResponseStatus' to Clr type 'String'.</code></p>
  
  <p>I don't believe nobody did this before, so I wonder what am I missing?</p>