XML serialization and empty collections

I have a property defined as:

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

If List <> Delete has no items

<delete />

. If List <> Delete is set to null

<delete xsi:nil="true" />

. Is there a way to use attributes to remove a delete item if it has no items?

Greg - Great thanks, I didn't even read the IsNullable documentation, just assuming it signals this as not required.

Rob Cooper - ISerializable, . , (1), , null, . , EventsBuilder (, , Events), / Events, Events .

+3
4

IsNullable = false ( false), "delete" . , .

, "nullability" .NET , NULL XML - , xml: nil. XmlArrayAttribute.IsNullable .

+6

, , 0. XML xsi: null = "true" ( ).

- , bool , , , "", XMLSerializer , , .

, IXMLSerializer:

public List<Event> Delete { get; set; }
[XMLIgnore]
public bool DeleteSpecified
{
 get
 {
   bool isRendered = false;
   if (Delete != null)
   {
     isRendered = (Delete.Count > 0);
   } 

   return isRendered;
 }
 set
 {
 }
}
+17

, , : " ?".

XmlSerializer , , XML. , "" , , List, .

, :

  • Getter null, 0 . , .
  • IXmlSerializable .
  • , " XML " - , , , , , "", . , null/zero-item.

, .

+1

Source: https://habr.com/ru/post/1696927/


All Articles