XML Serialization in ASP.NET Web API

By default, ASP.NET Web API will serialize results in JSON format. To instruct it to serialize results in XML, we will need to have the following lines of code in the Application_Start event in Global.asax.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(
    new System.Net.Http.Formatting.XmlMediaTypeFormatter());
The XmlMediaTypeFormatter uses the DataContractSerializer by default. To use the XmlSerializer instead, add the following line:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

No comments:

Post a Comment

Popular Post