Xml Serialization - Render Empty Element

前端 未结 5 1445
说谎
说谎 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:44

    You could try adding the XMLElementAttribute like [XmlElement(IsNullable=true)] to that member and also set in the get/set property something like this:

    [XmlElement(IsNullable = true)] 
    public string Data 
    { 
        get { return string.IsNullOrEmpty(this.data) ? string.Empty : this.data; } 
        set 
        { 
            if (this.data != value) 
            { 
                this.data = value; 
            } 
        } 
    } 
    private string data;
    

    And so you will not have:

    
    

    You will have this on render:

    
    

提交回复
热议问题