How to get XML data from Web API

If you’ve come this far, I can only presume that you are well versed with RESTful Services. In any case, if you are new to REST, here’s the definition.

Definition:

Representational state transfer (REST) is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called RESTful Web services, provide interoperability between computer systems on the internet. RESTful Web services allow the requesting systems to access and manipulate textual representations of Web resources by using a uniform and predefined set of stateless operations. Other kinds of Web services, such as SOAP Web services, expose their own arbitrary sets of operations.

Content Negotiation:

Content negotiation in ASP.NET Web API is an intrinsic server-driven mechanism, used to determine, based on client’s request, which media type formatter is going to be used to return an API response.

Getting XML Data:

Assuming that you are using ASP.NET Core, in order to add XML support, following are some of the considerations to get XML data from a Web API.

Be sure to register XmlDataContractSerializerFormatter in ConfigureServices of your Startup class.

Please note, I am using Visual Studio 2019 and targeting ASP.NET Core 3.1.

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers()
                    .AddXmlDataContractSerializerFormatters();
        }

In addition to the HttpGet verb, you also need to specify the format in order to request XML data like so in your controller class:

[HttpGet(“get.{format}”), FormatFilter]

[Route("api/[controller]")]
    [ApiController]
    [FormatFilter]
    public class EmployeesController : ControllerBase
    {
        private readonly SSRSWebApiContext _context;

        public EmployeesController(SSRSWebApiContext context)
        {
            _context = context;
        }

        // GET: api/<EmployeesController>
        [HttpGet("get.{format}"), FormatFilter]
        public IEnumerable<Employee> Get()
        {
            return _context.Employees.ToList();
        }

        // GET api/<EmployeesController>/5
        [HttpGet("{id}/get.{format}"), FormatFilter]
        public Employee Get(int id)
        {
            var emp = _context.Employees.FirstOrDefault(e => e.ID == id);
            return emp;
        }

Content-Type: XML:

Gets all Employees with XML as the return data. In the URL below, right after the base address, we have defined the Route, i.e. api/employees followed by the Content-Type i.e. “get.xml”.

Content-Type: JSON:

Gets all Employees with JSON as return data. In the URL below, right after the base address, we have the Route, i.e. api/employees followed by the Content-Type i.e. “get.json”.

 

As you can see; through the power of content negotiation, you can easily toggle between JSON and XML format based on client’s needs.

Do I need XML?:

You probably don’t! Majority of the clients rely on JSON data for most of their daily needs. However, recently, I had a requirement where I needed to bind XML data to an SSRS report. I am sure, there are other reasons why the client might need XML over JSON content.

Conclusion:

As you saw, to request content-type of XML requires minimal development effort on your side, which in essence, requires configuring the middleware without the need to write a new functionality.

About Obi Oberoi

I am a Technology Aficionado who has passion for learning, speaking, reading, helping and hanging out with virtuosos!
This entry was posted in .NET, Web API and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.