I have a REStful WCF web service (using a substantially modified WCF Rest Starter Kit) and the data contracts are simple POCOs marked with [Serializable] and [XmlType] (with members tagged with [XmlElement] or [XmlAttribute] where appropriate).
Somewhere inside WCF an instance of XmlSerializer is created which generates the output with no indentation or spacing between XML nodes, which is fine for automated processes, but makes debugging harder as I have to manually format the XML output myself.
I want to use XmlWriterSettings so it will automatically format the XML before it gets sent down the pipe, but I can’t see where I could inject it.
I used Reflector to find where XmlSerializer is instantiated within WCF and it shows up inside a few nested internal classes isnide XmlSerializerOperationBehavior, but beyond that I’m stuck.
Ta!
The
XmlWriterSettingsobject isn’t passed to the constructor to theXmlSerializer, but to theXmlWriterwhich will be then passed to the serializer when it’s time to write the object out. The place where you can change that is a custom message encoder (responsible for converting between the XML Infoset in the message and the actual bytes in the wire). One good sample of a custom encoder which creates aXmlWriterinstance is the “Custom Text Encoder“.