Do anyone know how programatically add an [XmlIgnore]
attribute to a class property in c#?
I'd like to do this to have just one class with one or two fields to be serialized as I'd need at run time.
Many thanks in advance.
Do anyone know how programatically add an [XmlIgnore]
attribute to a class property in c#?
I'd like to do this to have just one class with one or two fields to be serialized as I'd need at run time.
Many thanks in advance.
It is possible to dynamically override XML serialization attributes by passing XmlAttributeOverrides object to XmlSerializer constructor.
XmlAttributes samplePropertyAttributes = new XmlAttributes();
samplePropertyAttributes.XmlIgnore = true;
XmlAttributeOverrides sampleClassAttributes = new XmlAttributeOverrides();
sampleClassAttributes.Add(typeof(SampleClass), "SampleProperty", samplePropertyAttributes);
var serializer = new XmlSerialized(typeof(SampleClass), sampleClassAttributes);
See XmlAttributeOverrides Class in MSDN for details.