XmlResult and FromXmlBody are ASP.NET Core MVC Xml formatters [Microsoft.AspNetCore.Mvc.Formatters.Xml] extensions. They allow precisely define which of the Xml formatters DataContractSerializer or/and XmlSerializer to use for input and output in the Web Application controller actions.
$ dotnet add package Microsoft.AspNetCore.Mvc.Formatters.Xml.ExtensionsVersion 10.x.x : supports only NetCore 10.0
Version 9.x.x : supports only NetCore 9.0
Version 8.x.x : supports only NetCore 8.0
Version 7.x.x : supports only NetCore 7.0
Version 6.x.x : supports only NetCore 6.0
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions
An Action result which formats the given object as Xml.
Specifies an action parameter or property that should be bound with using the HTTP request Xml body.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
// "AddXmlFormaterExtensions()" initialize the Asp .Net Core MVC to use of XmlResult and FromXmlBody:
// - It adds the XmlSerializer and DataContractSerializer formatters to MVC.
// - It adds the XmlResult and FromXmlBody Extension to MVC.
services.AddMvc().AddXmlFormaterExtensions();
// or services.AddControllers().AddXmlFormaterExtensions().AddNewtonsoftJson();
}
XmlExtController.cs(Example):
/// <summary>
/// The Controller example of using of XmlResult and FromXmlBody.
/// It demonstrates how to define which of the Xml formatters DataContractSerializer
/// or/and XmlSerializer to use for input and output in the Web Application controller actions.
/// </summary>
[Route("api/[controller]")]
public class XmlExtController : Controller
{
// GET api/[controller]/xml
[HttpGet("xml")]
public ActionResult GetXmlObject()
{
object obj = new PurchaseOrder();
return new XmlResult(obj);
}
// GET api/[controller]/dcxml
[HttpGet("dcxml")]
public ActionResult GetDcXmlObject()
{
object obj = new PurchaseOrder();
return new XmlResult(obj) { XmlSerializerType = XmlSerializerType.DataContractSerializer };
}
// POST api/[controller]/xml
[HttpPost("xml")]
public void PostXml([FromXmlBody]PurchaseOrder value)
{
var x = value;
x.billTo.street += " 123";
}
// POST api/[controller]/dcxml
[HttpPost("dcxml")]
public void PostDcXml([FromXmlBody(XmlSerializerType = XmlSerializerType.DataContractSerializer)]PurchaseOrder value)
{
var x = value;
x.billTo.street += "No -10";
}
}
Where the Models:
[DataContract (Namespace ="http://puchase.Interface.org/Purchase.Order")]
public class PurchaseOrder
{
public PurchaseOrder()
{
billTo = new Address() { street = "Bill to Address" };
shipTo = new Address() { street = "Ship to Address" };
}
[DataMember]
public Address billTo;
[DataMember]
public Address shipTo;
}
[DataContract(Namespace = "http://puchase.Interface.org/Purchase.Order.Address")]
public class Address
{
[DataMember]
public string street;
}