views:

4569

answers:

3

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?

+1  A: 

" This prevents using base.Read/WriteXml."

Typically, if the base-class implemented IXmlSerializable, you might make it a virtual method so that the concrete version is used. Typically, you'd also use explicit implementation (rather than public properties) - perhaps with some protected virtual methods for the implementation details (although keeping track of where the reader/writer is across different classes would be a nightmare).

AFAIK, there is no way to re-use XmlSerializer to write the base bits, while you add the derived bits. IXmlSerializable is all-or-nothing.

Marc Gravell
Coming from Java and it's dead easy serialization, i am a bit sad to hear this. I actually made the base class, which i luckily have access to, implement IXmlSerializable. It works, but it kind of hurts, because i'm writing code that's not supposed to exist.
mafutrct
Unless someone comes up with a new approach, this is the official answer.
mafutrct
+1  A: 

why not simply use XmlSerializer in your read/write function?

XmlSerializer s = new XmlSerializer(typeof(Foo));
s.Serialize(writer, base);
Ricky Lung
Because it had to be manually serialized for reasons I can't recall anymore. :)
mafutrct
+3  A: 

Improving mtlung's answer, why don't you use XmlSerializer? You can tune up your class with attribute so it can be serialized the way you want, and it's pretty simple to do.

using System.Xml.Serialization;

...

[XmlRoot("someclass")]
public class SomeClass
{
    [XmlAttribute("p01")]
    public int MyProperty01
    {
        get { ... }
    }

    [XmlArray("sometypes")]
    public SomeType[] MyProperty02
    {
        get { ... }
    }

    [XmlText]
    public int MyProperty03
    {
        get { ... }
    }

    public SomeClass()
    {
    }
}

Then, serializing and deserializing it would be pretty simple:

void Save(SomeClass obj)
{
    XmlSerializer xs = new XmlSerializer(typeof(SomeClass));
    using (FileStream fs = new FileStream("c:\\test.xml", ...))
    {
        xs.Serialize(fs, obj);
    }
}

void Load(out SomeClass obj)
{
    XmlSerializer xs = new XmlSerializer(typeof(SomeClass));
    using (FileStream fs = new FileStream("c:\\test.xml", ...))
    {
        obj = xs.Deserialize(fs);
    }
}

And the resulting XML would be something like this:

<someclass p01="...">
  <sometype>
    <!-- SomeType serialized objects as child elements -->
  </sometype>
  # value of "MyProperty03" as text #
</someclass>

This method works better with "POCO" classes, and it's simple and clean. You don't even have to use the attributes, they are there to help you customize the serialization.

Ricardo Nolde