Xml Serialization - Render Empty Element

前端 未结 5 1444
说谎
说谎 2021-02-07 03:44

I am using the XmlSerializer and have the following property in a class

public string Data { get; set; }

which I need to be output exactly like

5条回答
  •  不思量自难忘°
    2021-02-07 04:39

    The solution to this was to create a PropertyNameSpecified property that the serializer uses to determine whether to serialize the property or not. For example:

    public string Data { get; set; }
    
    [XmlIgnore]
    public bool DataSpecified 
    { 
       get { return !String.IsNullOrEmpty(Data); }
       set { return; } //The serializer requires a setter
    }
    

提交回复
热议问题