How to convert a stream to a string?

R

Robert Dufour

I got a stream from a file, could be any document type like doc or txt.

I want to convert it to a string variable so that I can replace some custom
tag contents in it's text with text values generated from a database.

Then I need to save the stream as another document file.

so far I got
Dim Fname As String = "C:\Myfile.doc"

Dim bytes As Byte()

Dim fs As FileStream = File.OpenRead(Fname)

That gets me a stream but how do I change this to a string variable?

Maybe better question is how would I detect as sequence of charracters that
would be like "<YOURCOMPANYNAME>" and replace it with " My company name is
".

Thanks for any help

Bob
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Robert said:
I got a stream from a file, could be any document type like doc or txt.

I want to convert it to a string variable so that I can replace some custom
tag contents in it's text with text values generated from a database.

Then I need to save the stream as another document file.

so far I got
Dim Fname As String = "C:\Myfile.doc"

Dim bytes As Byte()

Dim fs As FileStream = File.OpenRead(Fname)

That gets me a stream but how do I change this to a string variable?

Maybe better question is how would I detect as sequence of charracters that
would be like "<YOURCOMPANYNAME>" and replace it with " My company name is
".

Thanks for any help

Bob

If it's a text file, you can decode it into a string. If it's not, you
can't. In a doc file there are byte codes that can't be handled as text.

To decode the text, you have to know what encoding was used to create
it. If the encoding is utf-8, you can use the Encoding.UTF8.GetString
method to decode the byte array into a string.
 
O

\(O\)enone

Robert said:
I got a stream from a file, could be any document type like doc or
txt.
I want to convert it to a string variable so that I can replace some
custom tag contents in it's text with text values generated from a
database.

I think you may face extreme difficulty doing this for many types of file
(including .doc files). Here are a couple of reasons:

1. Any binary file (.doc, .xls, etc.) is more than likely to have internal
pointers to parts of the document. If the replacement text is not EXACTLY
the same length as the original text, you'll corrupt the file as the
pointers will no longer be pointing at the right place.

2. Text may be encoded in a format other than plain ASCII, such as Unicode,
which will make searching for it much more difficult. You'll also need to
ensure that the replacement text is encoded in exactly the same way as the
source text.

You should be fine if you use a file that is basically text in structure
(such as .rtf, .txt, .csv, etc.) but I think you'll struggle with this
approach for most other file types.
 

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