ASP.NET XML serialisation issues: Observations on DataContractSerializer
Tags: dotnet, csharp, xml, serialization
Published: 2021-11-13 03:59:18

---
title: ASP.NET XML serialisation issues: Observations on DataContractSerializer 
published: true
description: A client reached out to us with a weird problem. Their ASP.NET WebAPI project (somewhat legacy tech) needed to communicate with an application running on a mainframe (dinosaur-grade legacy tech). &hellip; Continue reading &#8220;ASP.NET XML serialisation issues: observations on DataContractSerializer&#8221;
tags: dotnet, csharp, xml, serialization
canonical_url: https://blog.wiseowls.co.nz/index.php/2021/11/12/asp-net-xml-serialisation-issues-observations-on-datacontractserializer/
cover_image: https://blog.wiseowls.co.nz/wp-content/uploads/2020/02/helloquence-5fNmWej4tAA-unsplash-scaled-e1581823690656.jpg
---

A client reached out to us with a weird problem. Their ASP.NET WebAPI project (somewhat legacy tech) needed to communicate with an application running on a mainframe (dinosaur-grade legacy tech). But they were having XML serialisation issues…

They had a test XML payload, but only half of that kept coming across the wire:

![broken serialisation – missing fields](https://blog.wiseowls.co.nz/wp-content/uploads/2021/11/image.png) first thing we suspected was missing `DataContract`/`DataMember` attributes, but everything seemed to be okay:

```csharp
[DataContract(Name = "ComplexObject", Namespace = "")]
public class ComplexObject
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Location { get; set; }
    [DataMember]
    public string Reference { get; set; }
    [DataMember]
    public double Rate { get; set; }
    [DataMember]
    public double DiscountedRate { get; set; }
}
```


After scratching our heads for a little while and trying different solution from across the entirety of StackOverflow, we managed to dig up a [piece of documentation](https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-member-order) that explained this behaviour:

1. Data members of base classes (assuming serialiser will apply these rules upwards recursively);
2. Data members in alphabetical order **(bingo!)**;
3. Data members specifically numbered in decorating attribute;

With the above in mind, we got the following payload to serialise successfully:

![successful serialisation](https://blog.wiseowls.co.nz/wp-content/uploads/2021/11/image-1.png) There are other options
-----------------------

.NET comes with at least two XML serialisers: [XmlSerializer](https://docs.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netframework-1.1) and [DataContractSerializer](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=netframework-3.0). A lot has been written about the two. We find this [article written by Dan Rigsby](http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/) to probably be the best source of information on the topic.

Key difference for us was the fact that `XmlSerializer` does not require any decorations and works out of the box. While `DataContractSerializer` needs us to make code changes. In our project everything was already set up with `DataContract`, so we did not have to change anything.

By default, WebAPI projects come configured to leverage `DataContractSerializer`. It however pays to know that in case of any issues we can switch to use `XMLSerializer`:

```
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Formatters.XmlFormatter.UseXmlSerializer = true; // global setting for all types
        config.Formatters.XmlFormatter.SetSerializer<ComplexObject>(new XmlSerializer(typeof(ComplexObject))); // overriding just for one type
```


Setting order 
--------------

Yet another option to deal with ASP.NET XML serialisation issues would be to define property order explicitly:

```
[DataContract(Name = "ComplexObject", Namespace = "")]
public class ComplexObject
{
    [DataMember(Order = 1)]
    public int Id { get; set; }
    [DataMember(Order = 2)]
    public string Name { get; set; }
    [DataMember(Order = 3)]
    public string Location { get; set; }
    [DataMember(Order = 4)]
    public string Reference { get; set; }
    [DataMember(Order = 5)]
    public double Rate { get; set; }
    [DataMember(Order = 6)]
    public double DiscountedRate { get; set; }
}
```


Conclusion
----------

XML serialisation has been around since the beginning of .NET. And even though it may seem that JSON has taken over, XML isn’t going anywhere any time soon. It is good to know we have many ways to deal with it should we ever need to.