Xmlserializer does not serialize base classes

I am having this very strange problem trying to serialize a logging class using XmlSerializer. The code was generated using the wsdl.exe tool. A class that is serialized is declared as follows:

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "xxxxx")]
public partial class InheritedRequestA : BaseRequest
{
}

Serialization of other classes that also inherit from BaseRequest includes all non-inherited members, but not one of the public members from BaseRequest. BaseRequest is declared as follows.

[System.Xml.Serialization.XmlIncludeAttribute(typeof(InheritedRequestA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(InheritedRequestB))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "xxxxx")]
public partial class BaseRequest
{
//members here
}

To serialize requests and responses together, I wrote a very simple Wrapper class that simply contains a request object and a response object. Code for serialization:

        XmlSerializer serializer = new XmlSerializer(typeof(Wrapper));
        string serializedObject = string.Empty;
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.Serialize(stream, wrapper);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                serializedObject = reader.ReadToEnd();
            }
        }

Any thoughts on why public properties inherited from the base class do not receive serialization would be greatly appreciated.

EDIT: . ActivatorWrapper VersionRetrieverWrapper.

[Serializable]
[XmlInclude(typeof(Wrapper))]
[XmlInclude(typeof(ActivatorWrapper))]
[XmlInclude(typeof(VersionRetrieverWrapper))]
public class Wrapper
{
}

[Serializable]
public class VersionRetrieverWrapper : Wrapper
{
    public InheritedRequestA Request { get; set; }
    public InheritedResponseA Response { get; set; }
}
+3
2

, BaseRequest ( , ). , XmlSerializer , (int? bool?) XML IsNullable true ([XmlElement(IsNullable = true)]).

+5

, Request/Response . :

class Program
{
    static void Main(string[] args)
    {
        var wrapper = new VersionRetrieverWrapper();
        wrapper.Request = new InheritedRequestA();
        wrapper.Request.Member = "Request";
        wrapper.Response = new InheritedResponseA();
        wrapper.Response.Member = "Response";

        Console.WriteLine(Save(wrapper));
    }

    public static string Save(Wrapper wrapper)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Wrapper));

        string serializedObject = string.Empty;
        using (MemoryStream stream = new MemoryStream())
        {
            serializer.Serialize(stream, wrapper);
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream))
            {
                serializedObject = reader.ReadToEnd();
            }
        }
        return serializedObject;
    }
}
public partial class InheritedRequestA : BaseRequest
{
}

public partial class InheritedResponseA : BaseRequest
{
}
public partial class BaseRequest
{
    //members here
    public string Member;
}

[XmlInclude(typeof(VersionRetrieverWrapper))]
public class Wrapper
{
}

public class VersionRetrieverWrapper : Wrapper
{
    public InheritedRequestA Request { get; set; }
    public InheritedResponseA Response { get; set; }
}
0

Source: https://habr.com/ru/post/1720101/


All Articles