Mapping unmapped XML elements

Not all API’s return JSON, XML is still here and I’m still learning things about XML serialization.

Building with WCF back in the day, I learned about IExtensibleDataObject, a way to receive and return unmapped XML elements.
This can help forwards compatibility with your DataContracts, a client won’t discard unknown properties.

Store extra data encountered by the XmlObjectSerializer during deserialization of a DataContract

IExtensibleDataObject

We came across exactly this problem, not a SOAP API, but a REST API that returns XML We as a client only used a subset of an XML API contract, but had to PUT back all elements in an update, or properties would be reset on the server.

I rembered IExtensibleDataObject, but this was no help since we didn’t use the DataContract attributes/XmlObjectSerializer approach. Instead classes are marked with XmlElementAttribute and so on.

Just before we were about to go the custom solution way, a colleague came accross XmlAnyElementAttribute.

This member contains objects that represent any XML element that has no corresponding member in the object being serialized or deserialized

XmlAnyElementAttribute

How nice is this, you add a property decorated with this attribute to capture all unmapped/unknown properties, and you can post them back to the server without losing anything.

Theme music for this blogpost

Mapping unmapped XML elements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s