Search and replace inside a stream

?

....

Hi
I have an existing function which has a stream object (inmsg.BodyPart.Data).
I'm trying to search and replace the stream object in the most efficient way
possible

This is my attempt below, however I'm getting a message on the last line
that I can't convert a memorystream to a stream. Any hints, or better ways
of doing this?
Thanks in advance

StreamReader ms = new StreamReader(inmsg.BodyPart.Data);
string content = string.Empty;
content = ms.ReadToEnd();
content = content.Replace("HELLO","GOODBYE");
content = content.Replace("YES","NO");
inmsg.BodyPart.Data = ms;
 
M

Marc Gravell

What is the *exact* message? MemoryStream *is* a Stream, so this isn't quite
the issue; perhaps this is a read-only property?

Marc
 
M

Marc Gravell

Also - just in terms of efficiency : it would seem preferable to read one
line at a time and work with each?

And - not all stream implementations lend themselves to this type of usage;
typically a stream is strictly input or output. I don't recognise the
objects (are they your own?), but it could well be that amending the stream
achieves nothing, as this is actually representing the data flowing /out/ of
somewhere...
 
G

Guest

If immsg.BodyPart.Data is a stream, what you are doing is reading it into a
string (content). Then you are performing a string Replace on this string.
Remember, the string is a completely separate object from what was in the
stream.
So finally, you are attempting to assign "ms" which is a StreamReader, to
the BodyPart.Data object which we've already discovered is not a
StreamReader, but a Stream. Meanwhile, completely separate and distinct from
this, you have a string, "content" - which you have altered. Does this make
sense?
Peter
 
?

....

They are actually part of a Biztalk custom pipeline, the inmsg.bodypart.data
is allowed to be change (that's actually the point of creating this custom
pipeline), my c# knowledge is letting me down though on how to manipulate
that stream (to simply search and replace)

Thanks for your quick response
 
M

Marc Gravell

As Peter observed, the real issue is mixing streams and readers; the
following (untested) is possibly nearer the mark? Note that you might need
to protect "output" to stop "writer" from Dispose()ing it too early (can't
remember if MemoryStream literally ignores Dispose(); if it does you should
be OK). If you do, there is a good wrapper for non-closing streams on Jon
Skeet's web-site.

public class SomeObject {
public Stream Stream;

public static void DoSomet(SomeObject obj) {
using(MemoryStream output = new MemoryStream()) {
using(StreamWriter writer = new StreamWriter(output))
using(StreamReader reader = new StreamReader(obj.Stream)) {
while(!reader.EndOfStream) {
string line = reader.ReadLine();
line= line.Replace("HELLO","GOODBYE");
line = line.Replace("YES","NO");
writer.WriteLine(line);
}
writer.Flush();
writer.Close();
reader.Close();
}
output.Position = 0; // rewind
obj.Stream = output;
}
}
}
 
?

....

Yes I noticed how stupid my code was, my latest code is below, feel like I'm
getting closer, but it's now acting very strange. The last
console.writeline returns the length of the stream has 88, but the file
which ultimatly gets created by biztalk is blank.

I've posted on the biztalk site, so maybe this isn't a C# issue anymore.


StreamReader ms = new StreamReader(inmsg.BodyPart.Data);
MemoryStream mms = new MemoryStream();

string content = string.Empty;

content = ms.ReadToEnd();
content = content.Replace("<cdatasection>", "<![CDATA[");
content = content.Replace("</cdatasection>", " ]]>");

byte[] imgarray = StrToByteArray(content);
mms.Write(imgarray,0,imgarray.Length);

inmsg.BodyPart.Data = mms;

Console.WriteLine(mms.Length);
Console.WriteLine(content);
Console.WriteLine("HELLO");
Console.WriteLine(imgarray.Length);
Console.WriteLine("LENGTH OF INMSG = ");
Console.WriteLine(inmsg.BodyPart.Data.Length);
return inmsg;
 
M

Marc Gravell

As per my previous post - try rewinding the memory stream; I imagine the
back-end is assuming the stream is at the start of the data (to be saved),
not the end...

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top