我在尝试使用XmlSerializer进行日志序列化时遇到了这个非常奇怪的问题。该代码由wsdl.exe工具生成。这被序列化作为被声明为类如下:Xmlserializer没有序列化基类成员
[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
}
为了序列的请求和响应一起,我写了只包含一个请求对象和响应对象一个非常基本的包装类。序列化代码:为何从基类继承的公共属性没有得到连载
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'的定义吗?我怀疑它缺少一些序列化属性。 – 2009-10-14 17:41:28