views:

83

answers:

1

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

What was that magic name?

public class MyClass {
    public List<int> MyList{ get; set; }
    public bool MyListMagicName() { return MyList.Count != 0; }
    public MyClass() {  MyList = new List<int>(); }
}
+2  A: 

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

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

Konamiman
Markdown comment: I wanted the "ShouldSerialize" part to be bold and the "PropertyName" part to be bold and italics, not surrounded by asterisks. Funny enough, the preview window shown while in edit mode shows the intended formatting!
Konamiman
See also: http://www.devolutions.net/articles/serialization.aspx#S222
Ian Kemp