I'm using XmlSerializer to serialize my object model to XML. At the moment just prior to serilization I'm recusively going through my entire model and rounding any values to 1 or 2 decimal places depending on some property.
Is there a way to do this by subclassing the XmlSerializer. I tried overriding the protected override void Serialize(object o, XmlSerializationWriter writer) method but it is never called.
Here is my block of code that does the serialisation:
StringWriter writer = new Utf8StringWriter();
XmlWriter xml = XmlWriter.Create(writer, new XmlWriterSettings() { Encoding = writer.Encoding });
new MySubClassSerializer(engine.GetType()).Serialize(xml, engine);
My subclassed serializer does not do anything at the moment just subclasses to it's parent.
What if you wrapped these values in another property? Like this:
[XmlIgnore()]
public float SomeValue { get; set; }
[XmlAttribute("SomeValue")]
public float SomeValueRounded
{
get { return (float)Math.Round(SomeValue, 2); }
set { SomeValue = value; }
}