If you want to change Xml, one of the ways is to use an XslTransform. I had a similar case, where I needed to remove the xmlns attributes from an Xml Post request.
In WCF you can 'intercept' the Xml messages before the go out, or before they are processed on the way in, by implementing either the IClientMessageInspector or the IDispatchMessageInspector, depending on whether you need this at the client or the server side.
For instance, to strip the namespace attributes from an outgoing Xml message to a web service, I implemented the IClientMessageInspector, using the following code:
#region IClientMessageInspector Members
public void AfterReceiveReply(ref Message reply, object correlationState)
{
//Console.WriteLine(reply.ToString());
}
private XslCompiledTransform xt = null;
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
Console.WriteLine(request.ToString());
if (!request.IsEmpty)
{
XmlReader bodyReader =
request.GetReaderAtBodyContents().ReadSubtree();
MemoryStream ms = new MemoryStream();
XmlWriter xw = XmlWriter.Create(ms);
if (xt == null)
{
xt = new XslCompiledTransform(true);
xt.Load("StripXmlnsi.xslt");
}
xt.Transform(bodyReader, xw);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
bodyReader = XmlReader.Create(ms);
Message changedMessage = Message.CreateMessage(request.Version, null, bodyReader);
changedMessage.Headers.CopyHeadersFrom(request.Headers);
changedMessage.Properties.CopyProperties(request.Properties);
request = changedMessage;
}
return null;
}
#endregion
and used the following transform:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<!-- remove element prefix (if any) -->
<xsl:element name="{local-name()}">
<!-- process attributes -->
<xsl:for-each select="@*">
<!-- remove attribute prefix (if any) -->
<xsl:attribute name="{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Hope this is helpfull.