简体   繁体   English

C#转换FileStream.WriteLine转到MemoryStream

[英]C# Convert FileStream.WriteLine to go to a MemoryStream

I wrote some code in a console program and tested with files. 我在控制台程序中编写了一些代码并使用文件进行了测试。 Now I want to port it to a BizTalk Pipeline Component that implements a specific interface. 现在我想将它移植到实现特定接口的BizTalk管道组件。 I wasn't aware that that .Write and .WriteLine methods from a File to a Memory Stream were so different. 我不知道,那.Write.WriteLine从文件到内存流的方法是如此不同。 I thought I would just be able to swap my objects. 我以为我能够交换我的对象。 There is no .WriteLine method, and the .Write method requires offset and bytes (additional parameters). 没有.WriteLine方法,而.WriteLine方法需要偏移量和字节(附加参数)。

So now, what is the best way to change my tested code to write to the memory stream, given that I have a lot of .WriteLine statements. 那么现在,考虑到我有很多.WriteLine语句,更改我测试代码以写入内存流的最佳方法是什么。 I could write to a StringBuffer first, but then I think that would blow the concept of streaming (ie would have the whole document in memory at one time). 我可以先写一个StringBuffer,然后我认为这会破坏流的概念(即将整个文档同时存储在内存中)。

// This is how I used the streams in the Console program 
//FileStream originalStream = File.Open(inFilename, FileMode.Open);
//StreamWriter streamToReturn = new StreamWriter(outFilename);

// This is how to get the input stream in the BizTalk Pipeline Componenet 
System.IO.Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();

MemoryStream streamToReturn = new MemoryStream();
streamToReturn.WriteLine("<" + schemaStructure.rootElement + ">");

There's a lot more code not shown here. 这里没有显示更多代码。 Above is just to set the stage for what I did. 以上只是为我所做的事情奠定了基础。

Use a StreamWriter which you can use to call WriteLine. 使用可用于调用WriteLine的StreamWriter

MemoryStream streamToReturn = new MemoryStream();
var writer = new StreamWriter(streamToReturn);
writer.WriteLine("<" + schemaStructure.rootElement + ">");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM