I'm using XmlSerializer. A big class instance is being serialized, and that big one aggregates (I'd say pretty much owns) smaller classes with some inheritance involved. It appears that in base classes, some properties are crucial and are used to mirror their state when being serialized. But for some of their subclasses those properties should be suppressed, because other, more readable ones, were added.
Here's an example (not real code sample):
class Base
{
public int x { get; set; }
// ...other stuff...
}
class Derived1 : Base
{
// just some added functions
// it's perfectly fine serializing base.x
// ...other stuff...
}
class Derived2 : Base
{
public int y { get; set; } // will in the end be populating this.y
// and base.x, but is more readable and has more sense in XML
// ...other stuff...
}
class BigClass
{
public List<Derived1> List1 { get { return m_List1; } }
private List<Derived1> m_List1 = new List<Derived1>();
public List<Derived2> List2 { get { return m_List2; } }
private List<Derived2> m_List2 = new List<Derived2>();
// ...other stuff...
}
I've read this for the topic of using XmlAttributeOverrides
. Apparently, it works for situations like XmlSerializer s = new XmlSerializer(typeof(Derived2), overrides)
. However, when I try to use it with BigClass
:
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes ignore = new XmlAttributes { XmlIgnore = true };
overrides.Add(typeof(Derived2), "x", ignore);
XmlSerializer serializer = new XmlSerializer(typeof(BigClass), overrides);
// and, finally, save it
using (TextWriter tw = new StreamWriter("output.xml"))
{
serializer.Serialize(tw, SomeBigClassInstance);
}
the output still contains the x
property for both types. The List
s contain them by reference to the appropriate subclass, so it's not like XmlSerializer
cannot see their actual class. Despite the overrides, the sample xml output would look like
<?xml version="1.0" encoding="utf-8"?>
<BigClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<List1>
<Derived1>
<x>1</x>
<!-- other stuff -->
</Derived1>
</List1>
<List2>
<Derived2>
<x>2</x> <!-- unwanted guy -->
<y>20</y>
<!-- other stuff -->
</Derived2>
</List2>
<!-- other stuff -->
</BigClass>
So the question is: what am I doing wrong here? Does this method of suppressing unwanted properties only work when serializer is deliberately applied to the node of this class only?
Note. The savings in my real case are bigger than just one line. There are more properties to be hidden like this, and lists can contain decently large amount of items. The concern is readability and to some extent, manual modifiability of the resulting xml-files. Apparently, recalculating x
properties or deleting them by hand is tiresome enough to attempt such endeavor.
Update 1. Trying to add code like
overrides.Add(typeof(List<Derived2>), "x", ignore);
doesn't seem to fix it. There are actually more nodes from BigClass
down to Derived1/2
in the aggregation hierarchy. And adding them all from Derived2
up to the root didn't help. So I'm assuming this proposal won't fix it even in this simplified case. (Update 2. It won't).