title: XmlDictionaryReader changes the xml namespace alias
body: <p>I have following XML Input for a WCF service. Using XmlReader I am validating the message and replaces with a new message. During this process the xml namespace alias is changed from <code>xmlns:soapenv</code> to <code>xmlns:s</code></p>
  
  <p>What change need to be done in the following C# code in order to maintain the namespace alias while recreating the message?</p>
  
  <p>Refer <a href="https://stackoverflow.com/questions/4106196/wcf-message-body-showing-sbody-stream-sbody-after-modification">WCF message body showing &lt;s:Body&gt;... stream ...&lt;/s:Body&gt; after modification</a> for viewing the correct replaced message content. </p>
  
  <p>From <a href="http://blogs.msdn.com/b/endpoint/archive/2011/04/23/wcf-extensibility-message-inspectors.aspx" rel="nofollow noreferrer">WCF Extensibility – Message Inspectors</a></p>
  
  <blockquote>
    <p>WCF Message objects can only be “consumed” once – and “consumed” can mean read, written or copied. The message body is essentially a read-once stream, so once it’s consumed it cannot be used again. So if, in the inspector code, one were to read the message, the WCF runtime wouldn’t be able to reuse that message in the rest of its pipeline (i.e., to encode it to send as a reply or to parse it into operation parameters). So if the inspector code needs to read the message, it’s the responsibility of the inspector to recreate the message. </p>
  </blockquote>
  
  <p><strong>Input</strong></p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"&gt;
  &lt;soapenv:Header&gt;
  &lt;To soapenv:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"&gt;http://local:54956/Service1.svc&lt;/To&gt;
  &lt;Action soapenv:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"&gt;http://tempuri.org/IService1/GetData&lt;/Action&gt;
  &lt;/soapenv:Header&gt;
  &lt;soapenv:Body&gt;
    &lt;tem:GetData&gt;
       &lt;!--Optional:--&gt;
       &lt;tem:value&gt;4&lt;/tem:value&gt;
    &lt;/tem:GetData&gt;
  &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p><strong>CODE</strong></p>
  
  <pre><code> private void MyInspectorsValidateMessageBody(ref System.ServiceModel.Channels.Message message, bool isARequest)
      {
  
          string originalMessageText = message.ToString();
  
          if (!message.IsFault)
          {
              XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
              XmlReader bodyReader = message.GetReaderAtBodyContents().ReadSubtree();
  
              //Settings
              XmlReaderSettings wrapperSettings = new XmlReaderSettings();
              wrapperSettings.CloseInput = true;
              wrapperSettings.ValidationFlags = XmlSchemaValidationFlags.None;
              wrapperSettings.ValidationType = ValidationType.Schema;
  
              //Add a event handler for ValidationEventHandler of XmlReaderSettings
              //Validation happens while read of xml instance
              //wrapperSettings.ValidationEventHandler += new ValidationEventHandler(MyHandlerForXMLInspectionErrors);
  
              XmlReader wrappedReader = XmlReader.Create(bodyReader, wrapperSettings);
  
              this.isRequest = isARequest;
  
              MemoryStream memStream = new MemoryStream();
              XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateBinaryWriter(memStream);
              xdw.WriteNode(wrappedReader, false);
              xdw.Flush(); memStream.Position = 0;
  
              XmlDictionaryReader xdr = XmlDictionaryReader.CreateBinaryReader(memStream, quotas);
  
              //Reconstruct the message with the validated body
              Message replacedMessage = Message.CreateMessage(message.Version, null, xdr);
              replacedMessage.Headers.CopyHeadersFrom(message.Headers);
              replacedMessage.Properties.CopyProperties(message.Properties);
              message = replacedMessage;
  
              string replacedMessageText = replacedMessage.ToString();
  
          }
      }
  </code></pre>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;s:Header&gt;
  &lt;To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"&gt;http://local:54956/Service1.svc&lt;/To&gt;
  &lt;Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"&gt;http://tempuri.org/IService1/GetData&lt;/Action&gt;
  &lt;/s:Header&gt;
  &lt;s:Body&gt;... stream ...&lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>