Omit Properties from WebControl Serialization

Posted by Laramie on Stack Overflow See other posts from Stack Overflow or by Laramie
Published on 2010-06-14T18:25:35Z Indexed on 2010/06/14 18:42 UTC
Read the original article Hit count: 211

Filed under:
|
|

I have to serialize several objects inheriting from WebControl for database storage. These include several unecessary (to me) properties that I would prefer to omit from serialization. For example BackColor, BorderColor, etc.

Here is an example of an XML serialization of one of my controls inheriting from WebControl.

<Control xsi:type="SerializePanel">
        <ID>grCont</ID>
        <Controls />
        <BackColor />
        <BorderColor />
        <BorderWidth />
        <CssClass>grActVid bwText</CssClass>
        <ForeColor />
        <Height />
        <Width />
        ...
      </Control>

I have been trying to create a common base class for my controls that inherits from WebControl and uses the "xxxSpecified" trick to selectively choose not to serialize certain properties.

For example to ignore an empty BorderColor property, I'd expect

[XmlIgnore]    
public bool BorderColorSpecified()
{
    return !base.BorderColor.IsEmpty;
}

to work, but it's never called during serialization.

I've also tried it in the class to be serialized as well as the base class.

Since the classes themselves might change, I'd prefer not to have to create a custom serializer. Any ideas?

© Stack Overflow or respective owner

Related posts about c#

Related posts about xml-serialization