Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I will not use the same classes from your example, I'll give a little example of myself. </p> <p>You need to implement the method public bool ShouldSerialize*<em>Name</em>*() where Name is the name of the attribute of which you want to have the control of the serialization.</p> <p>For example, I have this class:</p> <pre><code>[Serializable] public class SerializeExample { public string ResultType { get; set; } public string Error { get; set; } public string Failure { get; set; } public List&lt;string&gt; Steps { get; set; } public bool ShouldSerializeError() { return "Error".Equals(ResultType); } public bool ShouldSerializeFailure() { return "Failure".Equals(ResultType); } public bool ShouldSerializeSteps() { return ShouldSerializeError() || ShouldSerializeFailure(); } } </code></pre> <p>If I have this class instance:</p> <pre><code>SerializeExample ex1 = new SerializeExample { ResultType = "Success" }; </code></pre> <p>Then, only this XML is serialized:</p> <pre><code>&lt;SerializeExample&gt; &lt;ResultType&gt;Success&lt;/ResultType&gt; &lt;/SerializeExample&gt; </code></pre> <p>If I create this other class and serialize it:</p> <pre><code>SerializeExample ex2 = new SerializeExample { ResultType = "Error", Error = "Invalid data from user", Steps = new List&lt;string&gt;() }; </code></pre> <p>Then, this XML is generated.</p> <pre><code>&lt;SerializeExample&gt; &lt;ResultType&gt;Error&lt;/ResultType&gt; &lt;Error&gt;Invalid data from user&lt;/Error&gt; &lt;Steps /&gt; &lt;/SerializeExample&gt; </code></pre>
 

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