Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions 5.0.0

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.

Install-Package Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions -Version 5.0.0
dotnet add package Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions --version 5.0.0
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions --version 5.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ASP.NET Core MVC Xml formatter extensions

Version 5.x.x : supports only Microsoft.AspNetCore.App 5.0-*

Version 3.1.x : supports only Microsoft.AspNetCore.App 3.1.0-*
Version 3.0.x : supports only Microsoft.AspNetCore.App 3.0.0-*

Nuget Package:

https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions

Development Supported by JetBrains Open Source Program.

ASP.NET Core MVC Xml formatter's extensions allow:

  1. ASP.NET MVC Core Web Application controller actions to control the Xml serialization type.
  2. Avoid the ObjectResult limitation to use only one type of MVC Xml serializer per ASP.NET MVC Core Web Application.
  3. Satisfy all possible Xml JAVA REST Web API and Xml .NET REST Web API communication scenarios.

XmlResult

An Action result which formats the given object as Xml.

  1. The XmlResult is the similar feature to JsonResult in the project "Microsoft.AspNetCore.Mvc.Formatters.Json".
  2. The property "XmlSerializerType" of the XmlResult defines which one of the MVC Xml formatters to use either XmlSerializer or DataContractSerializer.
  3. It allows to return Xml formatted response with using the HTTP Response Body.

"FromXmlBody"

Specifies an action parameter or property that should be bound with using the HTTP request Xml body.

  1. The FromBodyXmlAttribute is the similar attribute to FromBodyAttribute in the project "Microsoft.AspNetCore.Mvc".
  2. The property "XmlSerializerType" of the FromBodyXmlAttribute defines which one of the MVC Xml formatters to use either XmlSerializer or DataContractSerializer.

Example of using in the application:

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;
    }

ASP.NET Core MVC Xml formatter extensions

Version 5.x.x : supports only Microsoft.AspNetCore.App 5.0-*

Version 3.1.x : supports only Microsoft.AspNetCore.App 3.1.0-*
Version 3.0.x : supports only Microsoft.AspNetCore.App 3.0.0-*

Nuget Package:

https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions

Development Supported by JetBrains Open Source Program.

ASP.NET Core MVC Xml formatter's extensions allow:

  1. ASP.NET MVC Core Web Application controller actions to control the Xml serialization type.
  2. Avoid the ObjectResult limitation to use only one type of MVC Xml serializer per ASP.NET MVC Core Web Application.
  3. Satisfy all possible Xml JAVA REST Web API and Xml .NET REST Web API communication scenarios.

XmlResult

An Action result which formats the given object as Xml.

  1. The XmlResult is the similar feature to JsonResult in the project "Microsoft.AspNetCore.Mvc.Formatters.Json".
  2. The property "XmlSerializerType" of the XmlResult defines which one of the MVC Xml formatters to use either XmlSerializer or DataContractSerializer.
  3. It allows to return Xml formatted response with using the HTTP Response Body.

"FromXmlBody"

Specifies an action parameter or property that should be bound with using the HTTP request Xml body.

  1. The FromBodyXmlAttribute is the similar attribute to FromBodyAttribute in the project "Microsoft.AspNetCore.Mvc".
  2. The property "XmlSerializerType" of the FromBodyXmlAttribute defines which one of the MVC Xml formatters to use either XmlSerializer or DataContractSerializer.

Example of using in the application:

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;
    }

Release Notes

Released for  Microsoft.AspNetCore.App 5.0.0

  • net5.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions:

Package Downloads
GraphQl.Extensions
GraphQl Extensions. Contains Xlsx output formatter, csv output formatter
EY.Common.NlogCoreLib
Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
5.0.0 61 11/14/2020
3.1.0 17,256 1/11/2020
3.0.1 2,046 9/30/2019
2.2.0 40,516 12/11/2018
2.1.0 15,765 6/5/2018
2.0.0 9,898 8/28/2017
1.1.0.2 11,448 1/17/2017