An Avanade Blogging Community

Welcome to An Avanade Blogging Community Sign in | Join | Help
in Search

Avanate - Continously Integrating!

A Generic XML BodyWriter for WCF

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();
                         
}
                 
}
   
}
}

 

Published Wednesday, January 09, 2008 9:16 AM by avanate

Comments

No Comments
Anonymous comments are disabled

This Blog

Post Calendar

<January 2008>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

Syndication