How to modify text in memory stream?
I'm writing some text to a memory stream and need to modify it once in the memory stream. The reason I can't modify prior to adding to the memory stream is that it is from some 3rd party component. Here is an example:
ms = New MemoryStream
doc.Save(ms, SaveFormat.Html)
I need to do some modification to the resulting text that ends up in ms. Is there a way to convert the contents of ms into a string, and then back into a memory stream once modified?
Re: How to modify text in memory stream?
You already know how because you've probably done it many times before: a StreamReader. A StreamReader reads text from a Stream. It's usually a FileStream because the text usually comes from a file, but it can be any type of Stream you want. Likewise a StreamWriter can write text to any Stream. Just make sure that you set the Position to the beginning of the Stream before reading or writing.
Also, I'm not 100% sure what happens if you write less data than is already in the Stream. Maybe the overflow gets truncated and maybe it doesn't, so you should do some testing to see for your self.