XmlFormatter Class

Namespace
Kampute.HttpClient.Xml
Assembly
  • Kampute.HttpClient.dll

Definition

Reads and writes application/xml content with T:System.Xml.Serialization.XmlSerializer or DataContractSerializer.
public sealed class XmlFormatter : HttpContentFormatter
Inheritance

Remarks

The formatter also reads text/xml, which RFC 7303 registers with the same definition as application/xml, and application/problem+xml, the problem details format of RFC 9457 for error responses, and advertises both in the Accept header. It reads any other media type with the +xml structured syntax suffix, which RFC 7303 registers for XML media types, such as application/vnd.example+xml, but does not advertise it. It writes application/xml only.

The client reads a response with the first formatter in HttpRestClient.ContentFormatters that can read it, so a formatter for a specific +xml media type takes over that type only when it is added before this one.

The Serializer property selects the serializer. The same rule applies when a response is read, by the requested model type, and when a payload is written, by the runtime type of the payload. With the default, XmlSerializerKind.Auto, a type marked with T:System.Runtime.Serialization.DataContractAttribute or T:System.Runtime.Serialization.CollectionDataContractAttribute uses DataContractSerializer, and any other type uses T:System.Xml.Serialization.XmlSerializer.

Register the formatter with UseXml(this HttpRestClient, Action<XmlFormatter>). Change its properties before the client sends requests, because a registered formatter is shared by all requests of the client.

Constructors

XmlFormatter()

Initializes a new instance of the XmlFormatter class.
public XmlFormatter()

Properties

DataContractSettings

Gets or sets the settings of DataContractSerializer.
public DataContractSerializerSettings DataContractSettings { get; set; }

Property Value

DataContractSerializerSettings
The settings used in both directions when DataContractSerializer is selected, or null for its defaults. They have no effect when T:System.Xml.Serialization.XmlSerializer is selected.

Serializer

Gets or sets which serializer reads and writes XML.
public XmlSerializerKind Serializer { get; set; }

Property Value

XmlSerializerKind
The serializer choice. The default is XmlSerializerKind.Auto.

Methods

CanReadMediaType(string)

Determines whether this formatter can read content of the specified media type.
protected override bool CanReadMediaType(string mediaType)

Parameters

mediaType string
The media type of the content, without parameters.

Returns

bool
true if mediaType is one of the HttpContentFormatter.ReadableMediaTypes or has the +xml structured syntax suffix; otherwise, false.

CreateContent(object, string)

Creates XML content that carries the specified payload.
protected override HttpContent CreateContent(object payload, string mediaType)

Parameters

payload object
The object to write.
mediaType string
The media type of the content to create.

Returns

HttpContent
An XmlContent with the Serializer and DataContractSettings of this formatter.

ReadContentAsync(HttpContent, Type, CancellationToken)

Asynchronously reads an object of the specified type from XML content.
protected override Task<object> ReadContentAsync(HttpContent content, Type modelType, CancellationToken cancellationToken)

Parameters

content HttpContent
The HttpContent to read.
modelType Type
The type of the object to read.
cancellationToken CancellationToken
A token for canceling the operation.

Returns

Task<object>
A task that resolves to the object read from content.

Remarks

The content is decoded with the character set of its Content-Type header, or as UTF-8 if it has none.