<p>I have a a property defined as:</p>

<pre><code>[XmlArray("delete", IsNullable = true)]
[XmlArrayItem("contact", typeof(ContactEvent)),
 XmlArrayItem("sms", typeof(SmsEvent))]
public List&lt;Event&gt; Delete { get; set; }
</code></pre>

<p>If the List&lt;> Delete has no items</p>

<pre><code>&lt;delete /&gt;
</code></pre>

<p>is emitted. If the List&lt;> Delete is set to null</p>

<pre><code>&lt;delete xsi:nil="true" /&gt;
</code></pre>

<p>is emitted. Is there a way using attributes to get the delete element not to be emitted if the collection has no items?</p>

<p><a href="https://stackoverflow.com/questions/69296/xml-serialization-and-empty-collections#69407">Greg</a> - Perfect thanks, I didn't even read the IsNullable documentation just assumed it was signalling it as not required.</p>

<p><a href="https://stackoverflow.com/questions/69296/xml-serialization-and-empty-collections#69518">Rob Cooper</a> - I was trying to avoid ISerializable, but Gregs suggestion works. I did run into the problem you outlined in (1), I broke a bunch of code by just returning null if the collection was zero length. To get around this I created a EventsBuilder class (the class I am serializing is called Events) that managed all the lifetime/creation of the underlying objects of the Events class that spits our Events classes for serialization.</p>

[{'answer_id': 69315, 'author': 'Craig Eddy', 'author_id': 5557, 'author_profile': 'https://Stackoverflow.com/users/5557', 'pm_score': 0, 'selected': False, 'text': '<p>You could always implement IXmlSerializer and perform the serialization manually.</p>\n\n<p>See <a href="http://www.codeproject.com/KB/cs/IXmlSerializable.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cs/IXmlSerializable.aspx</a> for an example.</p>\n'}, {'answer_id': 69407, 'author': 'GregK', 'author_id': 8653, 'author_profile': 'https://Stackoverflow.com/users/8653', 'pm_score': 4, 'selected': True, 'text': '<p>If you set IsNullable=false or just remove it (it is false by default), then the "delete" element will not be emitted. This will work only if the collection equals to null.</p>\n\n<p>My guess is that there is a confusion between "nullability" in terms of .NET, and the one related to nullable elements in XML -- those that are marked by xml:nil attribute. XmlArrayAttribute.IsNullable property controls the latter.</p>\n'}, {'answer_id': 69518, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 1, 'selected': False, 'text': '<p>First off, I would say ask yourself "<strong>What is Serialization?</strong>".</p>\n\n<p>The <em>XmlSerializer</em> is doing exactly what it is supposed to be doing, persisting the current state of the object to XML. Now, I am not sure why the current behaviour is not "right" for you, since if you have initialized the List, then it <em>is</em> initialized.</p>\n\n<p>I think you have three options here:</p>\n\n<ol>\n<li>Add code to the Getter to return null if the collection has 0 items. This may mess up other code you have though.</li>\n<li>Implement the <em>IXmlSerializable</em> interface and do all the work yourself.</li>\n<li>If this is a common process, then you may want to look at my question "<a href="https://stackoverflow.com/questions/20084/xml-serialization-and-inherited-types">XML Serialization and Inherited Types</a>" - Yes, I know it deals with another issue, but it shows you how to create a generic intermediary serialization class that can then be "bolted on" to allow a serilization process to be encapsulated. You could create a similar class to deal with overriding the default process for null/zero-item collections.</li>\n</ol>\n\n<p>I hope this helps.</p>\n'}, {'answer_id': 811038, 'author': 'theahuramazda', 'author_id': 99290, 'author_profile': 'https://Stackoverflow.com/users/99290', 'pm_score': 4, 'selected': False, 'text': '<p>I\'ve had the same issue where I did not want an element outputted if the field is empty or 0.\nThe XML outputted could not use xsi:null="true" (by design).</p>\n\n<p>I\'ve read somewhere that if you include a property of type bool with the same name as the field you want to control but appended with \'Specified\', the XMLSerializer will check the return value of this property to determine if the corresponding field should be included.</p>\n\n<p>To achieve this without implementing IXMLSerializer:</p>\n\n<pre><code>public List&lt;Event&gt; Delete { get; set; }\n[XMLIgnore]\npublic bool DeleteSpecified\n{\n get\n {\n   bool isRendered = false;\n   if (Delete != null)\n   {\n     isRendered = (Delete.Count &gt; 0);\n   } \n\n   return isRendered;\n }\n set\n {\n }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/69296', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2281/']