change order of xmlserialization with inherited class from base class

27 Views Asked by At

I have 2 classes. A base class and a extended class that inherit from this base class. When I want to serialize the extended class the order isn't correct.

public class BaseClass
{
   [XmlElement("B", Order = 2)]
   public string? B { get; init; }

   [XmlElement("C", Order = 3)]
   public string? C { get; init; }
}

public class ExtClass : BaseClass
{
   [XmlElement("A", Order = 1)]
   public string? A { get; init; }
}

The output when I serialise this is: B C A

According to this msdn article that is expected behavior and in other old links they suggest you can write your own serializer to combat this issue.

Is there an easier solution available to solve this problem?

0

There are 0 best solutions below