Table of Contents

Codebelt.Extensions.Carter.AspNetCore.Xml Namespace

Add XML response capability to your Carter application with the Codebelt.Extensions.Carter.AspNetCore.Xml namespace. The XmlResponseNegotiator type, built on System.Xml.XmlWriter, produces standards-compliant XML responses through the Carter HttpResponse.Negotiate() pipeline.

This negotiator uses XmlFormatterOptions to configure XML writer settings, supported media types, and exception-descriptor formatting. It supports the Accept and Accept-Charset headers for content-type and encoding negotiation, and is suitable for legacy interoperability, SOA-style APIs, and enterprise integration scenarios.

Availability: .NET 10

When to use

Choose XmlResponseNegotiator when your API must interoperate with legacy systems that require XML, when the API contract is defined by XSD schemas, or when you need standards-compliant XML output for enterprise integration workflows (e.g., SOAP-adjacent services, BizTalk, or SAP connectors).

Start here

Install the Codebelt.Extensions.Carter.AspNetCore.Xml NuGet package, then register XmlResponseNegotiator as the response negotiator in your Carter configuration via CarterConfigurator.WithResponseNegotiator<T>().

Classes

XmlResponseNegotiator

The XmlResponseNegotiator integrates System.Xml XML serialization into the Carter response pipeline. Register it as the response negotiator to have all HttpResponse.Negotiate() calls produce standards-compliant XML output—suitable for legacy interoperability and enterprise integration scenarios.

Availability: .NET 10

Example

XmlResponseNegotiator is constructed with an IOptions<XmlFormatterOptions> that controls supported media types and System.Xml serialization settings, including writer encoding. Before serializing a model, the negotiator tests the client's Accept header via CanHandle; only when it returns true is Handle called to produce XML output. Handle resolves the character encoding from the Accept-Charset header (falling back to XmlFormatterOptions.Settings.Writer.Encoding), sets the Content-Type response header, and streams the serialized XML directly to the response body.

The following example constructs the negotiator with default options, confirms that the application/xml media type is accepted, then writes an anonymous object to an in-memory response stream and reads back the serialized output:

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Codebelt.Extensions.Carter.AspNetCore.Xml;
using Cuemon.Xml.Serialization.Formatters;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;

namespace DocfxExample;

public static class XmlResponseNegotiatorExample
{
    public static async Task DemonstrateAsync()
    {
        var options = Options.Create(new XmlFormatterOptions());
        var negotiator = new XmlResponseNegotiator(options);
        var acceptHeader = MediaTypeHeaderValue.Parse("application/xml");
        if (negotiator.CanHandle(acceptHeader))
        {
            var context = new DefaultHttpContext();
            context.Request.Headers.Accept = "application/xml";
            var model = new { Name = "XML", Value = 42 };
            await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None);
            context.Response.Body.Position = 0;
            using var reader = new StreamReader(context.Response.Body, Encoding.UTF8);
            Console.WriteLine(await reader.ReadToEndAsync());
        }
    }
}