I've got a base class which is compatible with XML serialization and a derived class which implements IXmlSerializable.
In this example, the base class does implement 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 ());
}
}
}
This produces this output:
<?xml version="1.0" encoding="utf-16"?><Bar><fooProp>42</fooProp></Bar>
However, i'd like to use a base class which does not implement IXmlSerializable. This prevents using base.Read/WriteXml
. The result will be:
<?xml version="1.0" encoding="utf-16"?><Bar />
Is there any way to still get the desired result?