2
[Serializable]
public class ListGroups
{
    public ListGroups()
    {
        this.MyList = new List<string>();
    }

    [XmlAttribute("mylist"), DefaultValue(typeof(List<string>), "")]
    public List<string> MyList { get; set; }

    [XmlAttribute("mybool"), DefaultValue(false)]
    public bool MyBool { get; set; }
}

MyBool - all OK.

how write DefaultValue for List MyList, if "DefaultValue(null)" must not write - this.MyList = new List(); create Empty List.

i write - DefaultValue(new List() {}] - dont work.

what to do? I have hide XmlAttribute on XML if List MyList is Empty.

nymezide
  • 21
  • 1
  • Are you sure you really want an XmlAttribute that is a list. Attributes are generally values. XmlElement can handle arrays and lists. – FishySwede Dec 19 '14 at 08:40
  • i have XML: or - but i need - or . hide mylist="" if mylist have 0 items. – nymezide Dec 19 '14 at 08:56
  • .Net has "dirty hack" ShouldSerialize to make what you want http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.80).aspx Just return false from method ShouldSerializeMyList in case, when you want to avoid serialization (for example for list "this","list","is","empty") – vitalygolub Dec 19 '14 at 10:01

0 Answers0