【发布时间】: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