简体   繁体   中英

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. I wasn't aware that that .Write and .WriteLine methods from a File to a Memory Stream were so different. 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).

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. 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).

// 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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