2

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?

4
  • What have you tried that didn't work? Commented Apr 12, 2017 at 16:44
  • I think you need to decorate the CustomerSubmission class with the name of the xml element. I do similar things with custom json serialisation/deserialisation formatters. For example you can decorate a prop with the name of the json prop that is incoming so I'd guess you can do something with that. Never done it with xml though. Take a look at this post: stackoverflow.com/questions/31490563/… Commented Apr 12, 2017 at 17:00
  • @MarkC. I had confirmed that the mapping worked for xml ok if the class and xml element were named the same, and that it worked for json even if they were different. I tried looking for an attribute to decorate the class with but initially didn't turn anything up as I was looking in the wrong namespace... Commented Apr 12, 2017 at 17:46
  • @DarthJam Brilliant, that's the one. I needed to add [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 :) Commented Apr 12, 2017 at 17:49

2 Answers 2

0

I think you need to decorate the CustomerSubmission class with the name of the xml element. I do similar things with custom json serialisation/deserialisation formatters. For example you can decorate a prop with the name of the json prop that is incoming so I'd guess you can do something with that. Never done it with xml though. Take a look at this post:

Deserialize Xml Using Same Object With Different Element Name

Sign up to request clarification or add additional context in comments.

Comments

0

Yes it's possible. You can use [XmlRoot("YourClassName")] for the class, and [XmlElement("ORDER_LIST")] for the fields.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.