I’m using the .NET serialization classes to XML serialize and log argument values that get passed to certain functions in my application. To this end I need a means to XML serialize the property values of any classes that get passes, but ignoring any properties that cannot be XML serialized (e.g. any Image type properties).
I could go through my classes and mark such properties with the [XmlIgnore] attribute, but ideally I’d like a serializer that just skips over such properties.
Is this achievable?
You could use reflection to dynamically create a
XmlAttributeOverridesobject to add theXmlIgnoreattribute on relevant properties. You just need to implement the logic to determine whether a given type is eligible for XML serialization, and browse the object graph recursively. When you’re done creating theXmlAttributeOverridesobject, just pass it to theXmlSerializerconstructor