title: Are there alternatives to DataContractSerializer, for POST marshalling in Web API, that do not impose ordering on XML elements?
body: <p>I have an existing Web API endpoint that we have, up until now, only supported when accessed via JSON.</p>
  <p>That is, we only supported its use when content-type was &quot;application/json&quot;. We are now trying to make it accept &quot;application/xml&quot;.</p>
  <p>And we've worked through a number of issues, with not having [DataContract] on the model, then with not specifying namespace=&quot;&quot; on [DataContract] for the model, etc. But we've worked through that, and we now have the XML marshalling correctly. ModelState is valid.</p>
  <p>But we're not getting all of the data.</p>
  <p>Our controller action:</p>
  <pre><code>[HttpPost, Route(&quot;&quot;)]
  public HttpResponseMessage PostNoteForTickets([FromBody] IEnumerable&lt;NoteDto&gt; dto)
  {
      ...
  }
  </code></pre>
  <p>Our NoteDto:</p>
  <pre><code>[DataContract(Namespace = &quot;&quot;)]
  public class NoteDto
  {
      [DataMember]
      public string TicketNumber { get; set; }
  
      [DataMember]
      public string CreatedBy { get; set; }
      
      [DataMember]
      public DateTime? CreatedDateTime { get; set; }
      
      [DataMember]
      public string Contents { get; set; }
  }
  </code></pre>
  <p>The XML we're sending:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;ArrayOfNoteDto&gt;
      &lt;NoteDto&gt;
          &lt;TicketNumber&gt;TestJob1&lt;/TicketNumber&gt;
          &lt;CreatedBy&gt;PostMan&lt;/CreatedBy&gt;
          &lt;CreatedDateTime&gt;2020-11-02T13:14:15.678&lt;/CreatedDateTime&gt;
          &lt;Contents&gt;This is a note&lt;/Contents&gt;
      &lt;/NoteDto&gt;
  &lt;/ArrayOfNoteDto&gt;
  </code></pre>
  <p>As I said, this is marshalling without errors</p>
  <p>Our IEnumerable dto is populated with an array of NoteDto containing one NoteDto object.</p>
  <p>And that one NoteDto object has TicketNumber set to &quot;TestJob1&quot;, but CreatedBy, CreatedDateTime, and Contents are null - when they most definitely weren't null in the sent XML.</p>
  <p>I've checked the raw content:</p>
  <pre><code>var rawContent = Request.Content.asString();
  </code></pre>
  <p>And all of the fields are present in the XML that is being sent into the controller. Why is one field being populated and the others not?</p>
  <hr />
  <p>Continuing to explore:</p>
  <p>If I specify the field order in the datacontract:</p>
  <pre><code>[DataContract(Namespace = &quot;&quot;)]
  public class NoteDto
  {
      [DataMember(Order = 1)]
      public string TicketNumber { get; set; }
  
      [DataMember(Order = 2)]
      public string CreatedBy { get; set; }
  
      [DataMember(Order = 3)]
      public DateTime? CreatedDateTime { get; set; }
  
      [DataMember(Order = 4)]
      public string Contents { get; set; }
  }
  </code></pre>
  <p>And then submit XML with its elements in that same order, all of the fields populate correctly. If I submit the XML in a different order, some of the fields end up null.</p>
  <p>This strikes me as a quite unreasonable restriction on our API. I really don't want to have to tell our customers that their clients aren't working because they're submitting their XML with the elements in the wrong order.</p>
  <p>Especially when XML does <em>not</em> require element ordering. It's something that <em>can</em> be required, in an XML schema, but very rarely is.</p>
  <p>But Web API seems to be forcing me to require element ordering in the XML that is submitted to the endpoints in my API, whether I want to or not.</p>
  <p>And that seems like such an absurd idea that I am very hesitant to think that I actually understand what is going on.</p>
  <p>Anyone have any ideas?</p>
  <hr />
  <p>Another bit of exploration.</p>
  <p>If I do not specify an order, and submit XML with the elements in alphabetic order, they all populate as desired. You only need to specify order in the DataContract if you want other than alphabetic.</p>
  <hr />
  <p>So, further exploration - as David pointed out in comments, this is specifically a behavior of Microsoft's DataContractSerializer.</p>
  <p>So that raises a question - are there alternatives to the DataContractSerializer that can be configured to handle Web API marshalling that do not impose ordering on the XML elements?</p>