Change how XmlSerializer serializes empty elements

I am using XmlSerializer. It serializes the object just fine, but the client requires empty elements in this format <star:Confirm/> . Serializing instead of serializing empty elements to <star:Confirm></star:Confirm> there is a way to change it to serialize how the client needs it.

+3
source share
2 answers

After I tried different things, I accidentally found myself on a solution. I set XmlElementAttribute.IsNullable to true, as in the previous answer.

 [System.Xml.Serialization.XmlElementAttribute(ElementName = "Confirm", IsNullable=true)] public ConfirmType Confirm { get { return this.confirmField; } set { this.confirmField = value; this.RaisePropertyChanged("Confirm"); } } 

Then, when you set the type of confirmation in the code, I used the default constructor instead of setting Confirm to null.

 retval.ConfirmBODDataArea.Confirm = new ConfirmType(); 

This is serialized as <star:Confirm/>

+7
source

You can try setting the XmlElementAttribute.IsNullable property to true . However, keep in mind that the xsi:nil="true" attribute will be output as a result.

+3
source

All Articles