Xml Serializer

Project Hosting
NuGet Package NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.

A custom written XML serializer.

Usage

endpointConfiguration.UseSerialization<XmlSerializer>();

Raw XML

In certain integration scenarios it may be necessary to bypass NServiceBus opinionated serialization format (essentially key/value pairs) and directly send custom XML structures over messaging. In order to do that declare one or multiple properties on the message contract as XDocument or XElement.

Message with XDocument

public class MessageWithXDocument :
    IMessage
{
    // name and casing must match the rootnode
    public XDocument nutrition { get; set; }
}

Payload with XDocument

<nutrition>
    <daily-values>
        <total-fat units='g'>65</total-fat>
        <saturated-fat units='g'>20</saturated-fat>
    </daily-values>
</nutrition>

Message with XElement

public class MessageWithXElement :
    IMessage
{
    // name and casing must match the rootnode
    public XElement nutrition { get; set; }
}

Payload with XElement

<nutrition>
    <daily-values>
        <total-fat units='g'>65</total-fat>
        <saturated-fat units='g'>20</saturated-fat>
    </daily-values>
</nutrition>

The caveat of this approach is that the serializer will wrap the data in an outer node being named after the name of the property. So in the examples above note the associated expected payloads.

To avoid that, for interoperability reasons, instruct the serializer to not wrap raw XML structures:

var serialization = endpointConfiguration.UseSerialization<XmlSerializer>();
serialization.DontWrapRawXml();
The name of the property on the message must exactly match the name of the root node in the XML structure in order to be able to correctly deserialize the no longer wrapped content.

Caveats

XML Attributes

The XML serializer in NServiceBus is a custom implementation. As such it does not support the standard .NET XML Attributes.

Unsupported Types

  • HashTable
  • Types with non-default constructors. So types must have a public constructor with no parameters.
  • ArrayList

Samples


Last modified