Gio
Gio

Reputation: 4229

Is it Possible to Dynamically Configure XML Serialization?

I have several classes that I serialize/deserialize, each with a number of properties, some of which I'd like dynamically use the "Xml.Serialization.XmlIgnore" attribute on. The idea being if I want to save specific property info, I manage it by setting/clearing a flag. Is that even possible?

Upvotes: 1

Views: 209

Answers (2)

Hans Passant
Hans Passant

Reputation: 942010

Yes, this is possible by using the XmlAttributesOverrides class. It lets you generate the attributes dynamically rather than specifying them in your source code. The MSDN Library article for the class has a good example.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

Is it possible to dynamically enable/disable a .net attribute?

No. Attributes are baked at the assembly metadata at compile time. You will need to implement a custom serialization.

Can you simply imagine the consequences of being able to add/remove attributes at runtime? You could provoke disasters by removing for example the Serializable attribute from the String class or setting the ComVisible attribute to false on this same class :-)

Upvotes: 2

Related Questions