BizTalk Modify/Add namespace custom pipeline component (Excecute method).

 public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
          
            IBaseMessagePart bodyPart = pInMsg.BodyPart;
            string targetNameSpace = “http://targetnamespace“;
            if (bodyPart != null)
            {
                Stream originalStream = bodyPart.GetOriginalDataStream();
                if (originalStream != null)
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.IgnoreComments = true;
                    settings.IgnoreProcessingInstructions = true;
                    settings.IgnoreWhitespace = true;
                    using (XmlReader reader = XmlReader.Create(originalStream, settings))
                    {

                        XmlWriterSettings ws = new XmlWriterSettings();
                        ws.Indent = true;
                        ws.Encoding = System.Text.Encoding.UTF8;
                        ws.ConformanceLevel = ConformanceLevel.Auto;

                        MemoryStream outputStream = new MemoryStream();
                        using (XmlWriter writer = XmlWriter.Create(outputStream, ws))
                        {
                            reader.Read();
                            if (reader.NodeType == XmlNodeType.XmlDeclaration)
                                reader.Read();

                            //Root Node
                            if (reader.NodeType == XmlNodeType.Element)
                            {

                                // Associate the the new root node with the new namespace
                                writer.WriteStartElement(reader.Name, targetNameSpace);
                               // Append the rest of the xml file (After the roor node)

                                writer.WriteRaw(reader.ReadInnerXml());
                            }
                        }
                        outputStream.Position = 0;
                        bodyPart.Data = outputStream;
                    }
                }
            }
            return pInMsg;
        
        }