Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell XmlSerializer to serialize properties with [DefautValue(...)] always?
    text
    copied!<p>I am using <code>DefaultValue</code> attribute for the proper <code>PropertyGrid</code> behavior (it shows values different from default in bold). Now if I want to serialize shown object with the use of <code>XmlSerializer</code> there will be no entries in xml-file for properties with default values.</p> <p><strong>What is the easiest way to tell XmlSerializer to serialize these still?</strong> </p> <p>I need that to support "versions", so when I change default value later in the code - serialized property gets value it had serialized with, not "latest" one. I can think about following:</p> <ul> <li>Override behavior of <code>PropertyGrid</code> (use custom attribute, so it will be ignoreed by <code>XmlSerializer</code>);</li> <li>Do sort of a custom xml-serialization, where ignore <code>DefaultValue</code>'s;</li> <li>Do something with object before passing it to <code>XmlSeriazer</code> so it won't contain <code>DefaultValue</code>'s anymore.</li> </ul> <p>But there is a chance I miss some secret property what allows to do it without much pain =D.</p> <p>Here is an example of what I want:</p> <pre><code> private bool _allowNegative = false; /// &lt;summary&gt; /// Get or set if negative results are allowed /// &lt;/summary&gt; [Category(CategoryAnalyse)] [Admin] [TypeConverter(typeof(ConverterBoolOnOff))] //[DefaultValue(false)] *1 public bool AllowNegative { get { return _allowNegative; } set { _allowNegative = value; ConfigBase.OnConfigChanged(); } } //public void ResetAllowNegative() { _allowNegative = false; } *2 //public bool ShouldSerializeAllowNegative() { return _allowNegative; } *3 //public bool ShouldSerializeAllowNegative() { return true; } *4 </code></pre> <p>If I uncomment (*1), then I have desired effect in <code>PropertyGrid</code> - properties with default values are displayed in normal text, otherwise text is bold. However <code>XmlSerializer</code> will <strong>NOT</strong> put properties with default value into xml-file and this is <strong>BAD</strong> (and I am trying to fix it).</p> <p>If I uncomment (*2) and (*3), then it's totally same as uncommenting (*1).</p> <p>If I uncomment (*2) and (*4), then <code>XmlSerializer</code> will always put properties into xml-file, but this happens because they <strong>do not have default value anymore</strong> and <code>PropertyGrid</code> shows all values in bold text.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload