我有一个与XML序列化兼容的基类和一个实现IXmlSerializable的派生类.
在此示例中,基类确实实现了IXmlSerializable:
using System.Diagnostics; using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; namespace XmlSerializationDerived { public class Foo { public int fooProp; public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { fooProp = int.Parse (reader.ReadElementString ("fooProp")); } public void WriteXml(XmlWriter writer) { writer.WriteElementString ("fooProp", fooProp.ToString ()); } } public class Bar : Foo, IXmlSerializable { public new void ReadXml(XmlReader reader) { base.ReadXml (reader); } public new void WriteXml(XmlWriter writer) { base.WriteXml (writer); } static void Main(string[] args) { StringBuilder sb = new StringBuilder (); XmlWriter writer = XmlWriter.Create (sb); Bar bar = new Bar (); bar.fooProp = 42; XmlSerializer serializer = new XmlSerializer (typeof (Bar)); serializer.Serialize (writer, bar); Debug.WriteLine (sb.ToString ()); } } }
这会产生以下输出:
42
但是,我想使用一个不实现IXmlSerializable 的基类.这可以防止使用base.Read/WriteXml
.结果将是:
还有什么方法可以得到理想的结果吗?