Prevent self closing tags in XmlSerializer when no data is present

后端 未结 5 956
孤街浪徒
孤街浪徒 2020-12-10 12:53

When I serialize the value : If there is no value present in for data then it\'s coming like below format.

  
        Acknowledged b         


        
5条回答
  •  情话喂你
    2020-12-10 13:57

    You can add a dummy field to prevent the self-closing element.

    [XmlText]
    public string datavalue= " ";
    

    Or if you want the code for your class then Your class should be like this.

    public class Notes
    {
       [XmlElement("Type")]
       public string typeName { get; set; }
    
       [XmlElement("Data")]
       private string _dataValue;
       public string dataValue {
          get {
              if(string.IsNullOrEmpty(_dataValue))
                 return " ";
              else
                 return _dataValue;
          }
          set {
              _dataValue = value;
          }
       }
    }
    

提交回复
热议问题