473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to convert a stream to a string?

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


Mar 1 '07 #1
2 6352
Robert Dufour wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 1 '07 #2
Robert Dufour wrote:
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.

--

(O)enone
Mar 1 '07 #3

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

Similar topics

4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
5
by: jk | last post by:
I'm having trouble converting a datatable into xml, with resonse.write to aspx. I'm basically converting vb code that saved a recordset into a stream into c#, but the format is wrong. I've tried...
2
by: Brian | last post by:
I have a string that I received from a web service call and I want to write it to a Stream. I cannot figure out how to access the string as a byte array and write it to the Stream. Any thoughts. I...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
6
by: hb | last post by:
Hi, Would you please give me some idea to convert/decode a Base 64 encoded GIF image string to a *.gif file in ASP.Net? Thank you hb
10
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
0
by: jeff_rowa | last post by:
Hi guyz, Could anyone please tell me how can I convert an array of floats to a string? I know that I can use this code to convert one float to a string but what is the best way for an array of...
11
by: Sudzzz | last post by:
Hi, I'm trying to convert a string something like this "{201,23,240,56,23,45,34,23}" into an array in C++ Please help. Thanks, Sudzzz
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.