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
XmlResponseNegotiatorintegrates System.Xml XML serialization into the Carter response pipeline. Register it as the response negotiator to have allHttpResponse.Negotiate()calls produce standards-compliant XML output—suitable for legacy interoperability and enterprise integration scenarios.Availability: .NET 10
Example
XmlResponseNegotiatoris constructed with anIOptions<XmlFormatterOptions>that controls supported media types and System.Xml serialization settings, including writer encoding. Before serializing a model, the negotiator tests the client'sAcceptheader viaCanHandle; only when it returnstrueisHandlecalled to produce XML output.Handleresolves the character encoding from theAccept-Charsetheader (falling back toXmlFormatterOptions.Settings.Writer.Encoding), sets theContent-Typeresponse header, and streams the serialized XML directly to the response body.The following example constructs the negotiator with default options, confirms that the
application/xmlmedia 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()); } } }