I am rewriting an API in .net core, which must be able to support input in both xml and json. I've already added the XmlSerializerFormatters in my startup class.
The previous version of the API received the input via HTTP POST in to a model named "XMLObject", and XML posted to the API had the root element <XMLObject> - Json posted to the API of course did not need a named root element.
In my new version of the API, I'd really rather not call my model XMLObject - for reasons I hope are obvious - but would still like to support XML with <XMLObject> as the root element.
So what I'm looking for is to have a class like this:
public class CustomerSubmission
{
public string Id { get; set; }
[Required]
public string Submitter { get; set; }
[Required]
public string EncodedData { get; set; }
}
And a method in my controller like this:
[HttpPost( "submissionURL" )]
public async Task<IActionResult> PostSubmission( [FromBody] CustomerSubmission Incoming )
{
//do something with Incoming.ID, Incoming.Submitter, etc...
}
And yet still allow the customer to post XML like this :
<XMLObject>
<Id>632174</Id>
<Submitter>Lorem Ipsum PLC</Submitter>
<EncodedData>7987428509348750983725.....</EncodedData>
</XMLObject>
How can I do this? Is it possible to map the xml root element to a different class name?
[XmlRoot("XMLObject")]from the System.Xml.Serialization namespace to the CustomerSubmission class - thankyou. If you want to add it as an answer I will accept it :)