The magic name to suppress XmlSerialization of an empty list

Given the correct "MagicName" (it was something like "CanSerialize"), the following code will suppress xml for empty lists.

What is this magic name?

public class MyClass { public List<int> MyList{ get; set; } public bool MyListMagicName() { return MyList.Count != 0; } public MyClass() { MyList = new List<int>(); } } 
+7
c # xml-serialization
source share
1 answer

I believe that you are referring to the ShouldSerialize*PropertyName* method naming convention, but AFAIK this does not apply to XML serialization, but to serialization of component properties in Windows Forms (maybe I'm wrong anyway). See here: http://msdn.microsoft.com/en-us/library/53b8022e%28VS.71%29.aspx

UPDATE It seems like it also works for XML serialization, but it is an undocumented function: http://horacegoescoding.blogspot.com/2009/04/using-shouldserialize-for-conditional.html

+7
source share

All Articles