Search code examples
c#xml-serializationxmlserializer

Complex type class not being serialized by XmlSerializer


So i have classes:

public class classA : BaseClass
{
    public classA(){}
}


public abstract class BaseClass
{
    public ComplexTypeClass Total { get; set; }
    public List<Item> Items {get; set; } 
    //some other properties
}


public class ComplexTypeClass : ValueObject<ComplexTypeClass>
{
    public ComplexTypeClass()
    {
    }
    public ComplexTypeClass(decimal p1, decimal p2, decimal p)
    {
        P1 = p1;
        P2 = p2;
        P3 = p3;
    }
    public decimal P1 { get; }
    public decimal P2 { get; }
    public decimal P3 { get; }

    //some methods and validations
}


public abstract class ValueObject<T> : IEquatable<T>, IValidatableObject where T : ValueObject<T>
{
    //all implemented interfaces methods
}

And then I try serialize classA, all properties and list are serialized properly, except the ComplexTypeClass property. For this property I get such line in xml <Total />.

All classes have [Serializable], [ComplexType], [XmlType] attributes. I have tried different combinations as well. I have tried using [XmlElement], [XmlRoot] on ComplexTypeClass property. However that did nothing.

I do have another complex type class, which does not have interfaces, and this class is serialized properly.

Does these interfaces has to do anything? Or am i missing something simple and small?

Edit: getting rid of interfaces did not help.


Solution

  • If any one stumbles on this sort of problem, as humble @Sinatr pointed out, ComplexTypeClass properties

    public decimal P1 { get; }
    public decimal P2 { get; }
    public decimal P3 { get; }
    

    didn't have set, so XmlSerializer could not repopulate this object