title | summary | reviewed | component | redirects | related | ||
---|---|---|---|---|---|---|---|
XML Serializer |
A custom XML serializer |
2025-01-17 |
Xml |
|
|
snippet: XmlSerialization
In integration scenarios where the sender is unable to add headers to the message, the serializer can infer the message type based on the root node of the XML payload. To take advantage of this, the name of the payload's root node must match the Type.FullName
of a message type known to the receiving endpoint.
This technique allows messages without any headers, including the NServiceBus.EnclosedMessageTypes
header, to be processed. This is demonstrated by the native integration with RabbitMQ sample.
In certain integration scenarios it may be necessary to bypass NServiceBus's opinionated serialization format (essentially key/value pairs) and directly send custom XML structures over messaging. In order to do that, declare one or more properties on the message contract as XDocument
or XElement
.
snippet: MessageWithXDocument
snippet: MessageWithXElement
Using XDocument
or XElement
message properties will wrap the raw XML data in an outer node named after the property. The node will have the message definitions above and the following XML content:
snippet: RawXmlContent
The full message payload might look like this:
snippet: RawXmlPayload
To avoid the extra node (e.g. for interoperability reasons), instruct the serializer not to wrap raw XML structures:
snippet: ConfigureRawXmlSerialization
This will change the payload as follows:
snippet: RawXmlNoWrapPayload
Note
The name of the property on the message must match the name of the root node in the XML structure exactly in order to be able to correctly deserialize the no longer wrapped content.
The XML serializer in NServiceBus is a custom implementation. As such it does not support the standard .NET XML attributes.