我有一个包含文本的流,现在我想在该流中编辑一些文本(替换一些值)。
什么是最有效的方法来做到这一点,所以不破坏流?我想在自定义管道组件中使用它BizTalk
。
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
string msg = "";
using (VirtualStream virtualStream = new VirtualStream(pInMsg.BodyPart.GetOriginalDataStream()))
{
using(StreamReader sr = new StreamReader(VirtualStream))
{
msg = sr.ReadToEnd();
}
// modify string here
msg = msg.replace("\r\n","");
while (msg.Contains(" <"))
msg = msg.Replace(" <", "<");
VirtualStream outStream = new VirtualStream();
StreamWriter sw = new StreamWriter(outStream, Encoding.Default);
sw.Write(msg);
sw.Flush();
outStream.Seek(0, SeekOrigin.Begin);
pInMsg.BodyPart.Data = outStream;
pContext.ResourceTracker.AddResource(outStream);
}
return pInMsg;
}
这是代码,但正如您所见,当我这样做时,我正在中断流sr.ReadToEnd()
。
有没有更好的方法来做到这一点?