I want to remove the empty XML tag from XML-based POST Request in ASP.NET Core WEB API. Empty XML tag troubled me to deserialize XML to Model using XmlSerializerFormatters.
<?xml version = "1.0" encoding="UTF-8"?>
<DetailAssessmentRequest>
<CaseId></CaseId>
...
<LeisureTimeActivities>
<LeisureTimeActivity>1</LeisureTimeActivity>
<LeisureTimeActivity/> //want to remove this
<LeisureTimeActivity>2</LeisureTimeActivity>
<LeisureTimeActivity/> //want to remove this
</LeisureTimeActivities>
<LeisureTimeActivityOthers></LeisureTimeActivityOthers> //want to remove this
...
<Observation>Comments</Observation>
</DetailAssessmentRequest>
I have already added XmlSerializerFormatters and XmlDataContractSerializerFormatters in my Startup.cs
services.AddMvc()
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters();
Here is my Model:
[Serializable]
public class DetailAssessmentRequest
{
public int CaseId { get; set; }
...
[XmlArray("LeisureTimeActivities"), DefaultValue(null)]
[XmlArrayItem("LeisureTimeActivity")]
public List<int> LeisureTimeActivities { get; set; }
public string LeisureTimeActivityOthers { get; set; }
...
public string Observation { get; set; }
}
Here is a working demo you could follow: