我在嘗試使用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