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
- object
- HttpContentFormatter
- XmlFormatter
Remarks
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
nullfor 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
mediaTypestring- The media type of the content, without parameters.
Returns
- bool
trueifmediaTypeis one of the HttpContentFormatter.ReadableMediaTypes or has the+xmlstructured syntax suffix; otherwise,false.
CreateContent(object, string)
Creates XML content that carries the specified payload.
protected override HttpContent CreateContent(object payload, string mediaType)Parameters
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
contentHttpContent- The HttpContent to read.
modelTypeType- The type of the object to read.
cancellationTokenCancellationToken- A token for canceling the operation.
Returns
Remarks
The content is decoded with the character set of its
Content-Type header, or as UTF-8 if it has none.

The formatter also reads
text/xml, which RFC 7303 registers with the same definition asapplication/xml, andapplication/problem+xml, the problem details format of RFC 9457 for error responses, and advertises both in theAcceptheader. It reads any other media type with the+xmlstructured syntax suffix, which RFC 7303 registers for XML media types, such asapplication/vnd.example+xml, but does not advertise it. It writesapplication/xmlonly.The client reads a response with the first formatter in HttpRestClient.ContentFormatters that can read it, so a formatter for a specific
+xmlmedia 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.