473,796 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in msg.BodyPart.Da ta);
string content = string.Empty;
content = ms.ReadToEnd();
content = content.Replace ("HELLO","GOODB YE");
content = content.Replace ("YES","NO") ;
inmsg.BodyPart. Data = ms;

Jun 8 '06 #1
7 31795
What is the *exact* message? MemoryStream *is* a Stream, so this isn't quite
the issue; perhaps this is a read-only property?

Marc
Jun 8 '06 #2
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...
Jun 8 '06 #3
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"...." wrote:
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(in msg.BodyPart.Da ta);
string content = string.Empty;
content = ms.ReadToEnd();
content = content.Replace ("HELLO","GOODB YE");
content = content.Replace ("YES","NO") ;
inmsg.BodyPart. Data = ms;

Jun 8 '06 #4
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
"Marc Gravell" <ma**********@g mail.com> wrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
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...

Jun 8 '06 #5
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(SomeObj ect obj) {
using(MemoryStr eam output = new MemoryStream()) {
using(StreamWri ter writer = new StreamWriter(ou tput))
using(StreamRea der reader = new StreamReader(ob j.Stream)) {
while(!reader.E ndOfStream) {
string line = reader.ReadLine ();
line= line.Replace("H ELLO","GOODBYE" );
line = line.Replace("Y ES","NO");
writer.WriteLin e(line);
}
writer.Flush();
writer.Close();
reader.Close();
}
output.Position = 0; // rewind
obj.Stream = output;
}
}
}
Jun 8 '06 #6
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.writeli ne 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(in msg.BodyPart.Da ta);
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(imgar ray,0,imgarray. Length);

inmsg.BodyPart. Data = mms;

Console.WriteLi ne(mms.Length);
Console.WriteLi ne(content);
Console.WriteLi ne("HELLO");
Console.WriteLi ne(imgarray.Len gth);
Console.WriteLi ne("LENGTH OF INMSG = ");
Console.WriteLi ne(inmsg.BodyPa rt.Data.Length) ;
return inmsg;

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:60******** *************** ***********@mic rosoft.com...
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"...." wrote:
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(in msg.BodyPart.Da ta);
string content = string.Empty;
content = ms.ReadToEnd();
content = content.Replace ("HELLO","GOODB YE");
content = content.Replace ("YES","NO") ;
inmsg.BodyPart. Data = ms;

Jun 8 '06 #7
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
Jun 9 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2910
by: Vamsee Krishna Gomatam | last post by:
Hello, I'm having some problems understanding Regexps in Python. I want to replace "<google>PHRASE</google>" with "<a href=http://www.google.com/search?q=PHRASE>PHRASE</a>" in a block of text. How can I achieve this in Python? Sorry for the naive question but the documentation is really bad :-( Regards, GVK
0
2051
by: Kostya Altuhov | last post by:
It is really a pain to use a completely different syntax each time I want to do a search or replace using regular expressions in VS IDE. Why don't you support the normal regex syntax, i.e. the one used by ..net Regex class (and virtually by everything else), in VS IDE for search and replace?
4
10201
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function filter() { var items = new Array("John", "Jane");
0
1204
by: DaveJG | last post by:
I have a couple of questions involving the search/replace in the IDE and regular expressions. Any help would be greatly appreciated. 1. How would I replace two line feeds with one? That is Var1 = 1 Var2 = 2 becomes
23
7002
by: SeaPlusPlus | last post by:
I want to convert large files of prose to xhtml and so I need a way to remove unwanted line wraps. So, I'm looking for a freebee editor that has the capability of searching for a single "carriage return/line feed" or "line feed/carriage return" and removing them. I quess what I need is an editor that allows non-printable characters in it's search strings. Does anyone know of on that will allow this? Thank you...
4
4840
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional way mean to secrife lots of time in asp.net. can any one give me such a expression in which i pass a data string and search word string and replace word string? if so plz help me out. i'm in badly need.
2
2386
by: Gregory S. Williamson | last post by:
Dear peoples, I've got a problem which seemed to be neatly solved by the use of schemas, and in fact it mostly works, but I have tried to go one step too far, perhaps. Rather than have the application do SET search_path TO f12057; SELECT * FROM parcel-owners WHERE ... ; SET search_path TO public;
2
5527
by: Michael Peters | last post by:
is there a way to replace a certain sequence of characters by line feed (vbCrLf ), for all text columns in a table, using Search+Replace? -Michael
1
2606
by: dhivyacse | last post by:
i! how to create search engine inside the website only without using database
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.