title: Biztalk 2010 Custom Pipeline Component returns binary
body: <p>I have created a custom pipeline component which transforms a complex excel spreadsheet to XML. The transformation works fine and I can write out the data to check.  However when I assign this data to the BodyPart.Data part of the inMsg or a new message I always get a routing failure.  When I look at the message in the admin console it appears that the body contains binary data (I presume the original excel) rather than the XML I have assigned - see screen shot below.  I have followed numerous tutorials and many different ways of doing this but always get the same result.</p>
  
  <p><img src="https://i.stack.imgur.com/fKtHL.png" alt="Binary Returned"></p>
  
  <p>My current code is:</p>
  
  <pre><code>public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
      {
  
  
          //make sure we have something
          if (inmsg == null || inmsg.BodyPart == null || inmsg.BodyPart.Data == null)
          {
              throw new ArgumentNullException("inmsg");
          }
  
          IBaseMessagePart bodyPart = inmsg.BodyPart;
  
          //create a temporary directory
          const string tempDir = @"C:\test\excel";
          if (!Directory.Exists(tempDir))
          {
              Directory.CreateDirectory(tempDir);
          }
  
          //get the input filename
          string inputFileName = Convert.ToString(inmsg.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties"));
  
  
          swTemp.WriteLine("inputFileName: " + inputFileName);
  
          //set path to write excel file
          string excelPath = tempDir + @"\" + Path.GetFileName(inputFileName);
          swTemp.WriteLine("excelPath: " + excelPath);
  
          //write the excel file to a temporary folder  
          bodyPart = inmsg.BodyPart;
          Stream inboundStream = bodyPart.GetOriginalDataStream();
          Stream outFile = File.Create(excelPath);
          inboundStream.CopyTo(outFile);
          outFile.Close();
  
          //process excel file to return XML
          var spreadsheet = new SpreadSheet();
          string strXmlOut = spreadsheet.ProcessWorkbook(excelPath);
  
          //now build an XML doc to hold this data
          XmlDocument xDoc = new XmlDocument();
          xDoc.LoadXml(strXmlOut);
  
          XmlDocument finalMsg = new XmlDocument();
          XmlElement xEle;
          xEle = finalMsg.CreateElement("ns0", "BizTalk_Test_Amey_Pipeline.textXML",
                                        "http://tempuri.org/INT018_Workbook.xsd");
          finalMsg.AppendChild(xEle);
  
          finalMsg.FirstChild.InnerXml = xDoc.FirstChild.InnerXml;
  
          //write xml to memory stream
          swTemp.WriteLine("Write xml to memory stream");
          MemoryStream streamXmlOut = new MemoryStream();
          finalMsg.Save(streamXmlOut);
          streamXmlOut.Position = 0;
  
  
          inmsg.BodyPart.Data = streamXmlOut;
          pc.ResourceTracker.AddResource(streamXmlOut);
  
  
          return inmsg;
      }
  </code></pre>