C # Convert FileStream.WriteLine to go to MemoryStream

I wrote the code in a console program and tested it with files. Now I want to port it to a BizTalk Pipeline component that implements a specific interface. I didn't know that the methods .Write

and .WriteLine

from file to memory stream were so different. I thought I could just swap my objects. There is .WriteLine

no method , and the .Write method requires an offset and bytes (optional parameters).

So what's the best way to change my tested code to write to a memory stream, given that I have a lot of .WriteLine statements. I could write a StringBuffer at first, but then I think it will blow away the concept of streaming (i.e. will have the entire document in memory in one go).

// 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 is a lot more code here not shown here. Above all, to lay the groundwork for what I have done.

+3


source to share


1 answer


Use StreamWriter which you can use to call WriteLine.



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

      

+6


source







All Articles