I'm going through a rather fun process of integrating with a Java app that uses POX style interaction to save information to a remote server and ran across the problem of writing a general clob of XML data to the Message. I ran across a number of demos that all relied on having a number of custom BodyWriters, essentially one per message you'd be writing and came up with this instead. Apologize for the nasty formatting in advance.
public
class XmlBodyWriter : BodyWriter
{
private string _xmlData;
public XmlBodyWriter( string xmlData ) : base( false
{
_xmlData = xmlData;
}
protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer){
using (MemoryStream buffer = new MemoryStream(_xmlData.Length))
{
using (StreamWriter streamWriter = new StreamWriter(buffer))
{
streamWriter.Write(_xmlData);
streamWriter.Flush();
buffer.Seek(0, SeekOrigin.Begin);
XmlReader reader = XmlReader.Create(buffer);
writer.WriteNode(reader, true);
writer.Flush();
}
}
}
}