ASP.NET Core: XML request and response

Petr Kostelanský | 13 March 2019
In ASP.NET Core is preferred to used JSON for request parameter and for the response. But sometimes we need to use XML in POST Body and get XML response.

Let's go step by step to configure ASP.NET Core API project (this code was tested under .NET Core 2.2 version). 

Add Nuget package Microsoft.AspNetCore.Mvc.Formatters.Xml or Microsoft.AspNetCore.App that already contains this xml formatter and also json formatter.

Add XML Serializer Formatter in Startup.cs class:

      services.AddMvc()
           .AddXmlSerializerFormatters()
           .AddXmlDataContractSerializerFormatters();

Then we create a controller that is not special in any way:

    [Route("api/persons")]
    [ApiController]
    public class PersonsController : ControllerBase
    {
        private readonly List persons;
        public PersonsController()
        {
            persons = new List
            {
                new Person { PersonId = 5, FirstName = "Name5", Surname = "Surname5" },
                new Person { PersonId = 7, FirstName = "Name7", Surname = "Surname7" },
                new Person { PersonId = 8, FirstName = "Name8", Surname = "Surname8"}
            };
        }

        // GET api/persons
        [HttpGet]
        public IActionResult Get()
        {
            return Ok(persons);
        }

        // GET api/persons/5
        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            var person = persons.FirstOrDefault(s => s.PersonId == id);
            if (person == null)
            {
                return NotFound();
            }

            return Ok(person);
        }

        // POST api/persons/create
        [HttpPost("create")]
        public IActionResult Post([FromBody] PersonEntryRequest person)
        {
            // ToDo: store person in DB

            // Return person Id
            var response = new PersonEntryRespose
            {
                PersonId = 5
            };

            return Ok(response);
        }
    }

Now we can start our API and make a request. You can use Postman to test API. 

To be able to send request body as XML we have to add header Content-Type with value application/xml and if we want to get a response in XML format we have to add header Accept application/xml cause the default is json. 

There is a definition of how to set up the request:

Create person

Request:

Url: http://localhost:60145/api/persons/create
Method: POST
Header:
  Accept application/xml
  Content-Type application/xml
Body:
<?xml version="1.0" encoding="UTF-8"?>
<PersonEntryRequest>
	<FirstName>My name</FirstName>
	<Surname>My surname</Surname>
</PersonEntryRequest>

Response:

<PersonEntryRespose xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <PersonId>5</PersonId>
</PersonEntryRespose>

Get person by id

Url: http://localhost:60145/api/persons/7
Method: GET
Headers:
  Accept: application/xml

Response:

<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <PersonId>7</PersonId>
    <FirstName>Name7</FirstName>
    <Surname>Surname7</Surname>
</Person>

Get all persons

Url: http://localhost:60145/api/persons
Method: GET
Headers:
  Accept: application/xml

Response:

<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Person>
        <PersonId>5</PersonId>
        <FirstName>Name5</FirstName>
        <Surname>Surname5</Surname>
    </Person>
    <Person>
        <PersonId>7</PersonId>
        <FirstName>Name7</FirstName>
        <Surname>Surname7</Surname>
    </Person>
    <Person>
        <PersonId>8</PersonId>
        <FirstName>Name8</FirstName>
        <Surname>Surname8</Surname>
    </Person>
</ArrayOfPerson>
Loading ads...