【问题标题】:Xmlserializer not serializing base class membersXmlserializer 不序列化基类成员
【发布时间】:2010-12-06 18:19:26
【问题描述】:

我在尝试使用 XmlSerializer 序列化用于记录的类时遇到了这个非常奇怪的问题。该代码由 wsdl.exe 工具生成。被序列化的类声明如下:

[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
{
}

同样继承自 BaseRequest 的其他类的序列化包括所有非继承成员,但不包括来自 BaseRequest 的公共成员。 BaseRequest 声明如下。

[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
}

为了将请求和响应序列化在一起,我编写了一个非常基本的 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();
            }
        }

任何关于为什么从基类继承的公共属性没有被序列化的想法将不胜感激。

编辑:这是包装类。我已将其细分为 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; }
}

【问题讨论】:

  • 你能发布Wrapper的定义吗?我怀疑它缺少一些序列化属性。

标签: c# .net serialization xml-serialization


【解决方案1】:

您需要确保为BaseRequest 的公共成员分配了值(无论是在默认构造函数中、在其声明中还是在您的服务代码中)。如果不是,XmlSerializer 将忽略它们,除非它们都可以为空 (int? bool?)并且将 XML IsNullable 属性设置为 true ([XmlElement(IsNullable = true)])。

【讨论】:

    【解决方案2】:

    如果请求/响应不为空,这可以正常工作。这是一个工作示例应用程序:

    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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-06
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      相关资源
      最近更新 更多