How to ignore a field/property during serialization

I’ve hit the problem of serializing a property for the last time. When you create a type that contains a non-serializable type, in my case a CultureInfo class, you cannot automatically serialize the type – you get issues about "does not contain a public constructor", "problem reflecting the type", etc. Unfortunately this isn’t caught at compile time and you have to wait until an attempt is made to serialize the containing object. What to do about it? The more complicated choice is to write some custom serialization code, but really, most of the time I just want to serialize pretty simple objects. So my preference is to mark the class as serializable and let the client choose how to serialize it, i.e. via Binary or Xml "serializers". The framework provides a couple of attributes for this, and this is where the gotcha lives.

The first attribute, [NonSerialized], should be applied to field members and informs the standard serialization methods to ignore the field. The second attribute, [System.Xml.Serialization.XmlIgnore], should be applied to public properties and informs the Xml Serializer to ignore the property. The combination of the two should be enough to stop the field from being serialized regardless of the serializer the client chooses to use.
Quick pseudo example:

[NonSerialized],
private SomeUnFriendlyType myType:

[System.Xml.Serialization.XmlIgnore]
public SomeUnFriendlyType MyType()
….

4 thoughts on “How to ignore a field/property during serialization

  1. JHILLMAR January 18, 2008 / 5:34 pm

    I used xsd.exe to create a class and then added some properties I used the pseudo example but it did not work custom properties were still being serialized.
    So far no solution except the [field:NonSerializable] for C# Unfortunately I am using vb.net

  2. Paulio January 19, 2008 / 9:29 am

    Hi Jhillmar, I\’m not sure what you\’re attempting to do, feel free to post (or send me a message) more details

  3. JHILLMAR March 4, 2008 / 2:10 pm

    Hi Paulo
    I Serialize a collection of objects from an xml file using the schema definition tool (xsd.exe); however I need to modify the code and that included adding some properties. The problem was that when I Deserialize my objects the new properties were new elements of my xml file. I need it a way to ignore the newly created properties.
    Your pseudo example works 🙂 I did not implemented correctly. once I realize that, the problem was solve
    Thank you for your response
    in VB.Net
    <System.Xml.Serialization.XmlIgnore()> _Public ReadOnly Property TraceLogList() As List(Of ActionTraceLogEntry)   Get     Return _itemTraceLog   End GetEnd Property

  4. Paulio March 6, 2008 / 9:04 pm

    Glad to hear it worked in the end!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s