How do I avoid writing the Byte Order Mark ?

P

pamela fluente

I am writing some text using something like:

Using FileStream As New FileStream(FileName, FileMode.Create)
Using StreamWriter As New StreamWriter(FileStream,
System.Text.Encoding.UTF8)

Try
StreamWriter.Write(Text)
FileStream.Flush()
....


QUESTION: How do I avoid writing the Byte Order Mark ?

It's for HTML pages: validator is complaining about the mark.

-P
 
?

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

pamela said:
I am writing some text using something like:

Using FileStream As New FileStream(FileName, FileMode.Create)
Using StreamWriter As New StreamWriter(FileStream,
System.Text.Encoding.UTF8)

Try
StreamWriter.Write(Text)
FileStream.Flush()
...


QUESTION: How do I avoid writing the Byte Order Mark ?

It's for HTML pages: validator is complaining about the mark.

Then the problem is not the writing of the file, but the reading of the
file. When the file the read, the BOM is not part of the file data.

If you read the file as a binary file instead of a text file, you don't
get an encoded string, you get a binary image of a unicode file.
 
P

pamela fluente

Then the problem is not the writing of the file, but the reading of the
file. When the file the read, the BOM is not part of the file data.

If you read the file as a binary file instead of a text file, you don't
get an encoded string, you get a binary image of a unicode file.

As I explained the file is read by browsers (it's a web page).

The validator message is:

Byte-Order Mark found in UTF-8 File.
The Unicode Byte-Order Mark (BOM) in UTF-8 encoded files is known to
cause problems for some text editors and older browsers. You may want
to consider avoiding its use until it is better supported.


I am looking for a way NOT to write the BOM, because the validator
complains.

How do avoid the BOM writing ?

-P
 
K

Kelly Ethridge

pamela said:
As I explained the file is read by browsers (it's a web page).

The validator message is:

Byte-Order Mark found in UTF-8 File.
The Unicode Byte-Order Mark (BOM) in UTF-8 encoded files is known to
cause problems for some text editors and older browsers. You may want
to consider avoiding its use until it is better supported.


I am looking for a way NOT to write the BOM, because the validator
complains.

How do avoid the BOM writing ?

-P

The default UTF8 encoding is set to write the preamble (BOM). Instead of
using System.Text.Encoding.UTF8, you should create a new UTF8Encoding
object. By default, a new one won't include the the preamble (BOM).
 
M

Michael Phillips, Jr.

I am looking for a way NOT to write the BOM, because the validator
complains.
How do avoid the BOM writing ?

Rewind the stream remove the BOM and seek to the end of the stream.
Use FileStream.Flush to commit the changes to the file.

Then the problem is not the writing of the file, but the reading of the
file. When the file the read, the BOM is not part of the file data.

If you read the file as a binary file instead of a text file, you don't
get an encoded string, you get a binary image of a unicode file.

As I explained the file is read by browsers (it's a web page).

The validator message is:

Byte-Order Mark found in UTF-8 File.
The Unicode Byte-Order Mark (BOM) in UTF-8 encoded files is known to
cause problems for some text editors and older browsers. You may want
to consider avoiding its use until it is better supported.


I am looking for a way NOT to write the BOM, because the validator
complains.

How do avoid the BOM writing ?

-P
 
P

pamela fluente

The default UTF8 encoding is set to write the preamble (BOM). Instead of
using System.Text.Encoding.UTF8, you should create a new UTF8Encoding
object. By default, a new one won't include the the preamble (BOM).- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

If this is true, it is a simple solution.

I will try that. I am wondering why a new object would behave
differently (?)

-P
 
K

Kelly Ethridge

pamela said:
If this is true, it is a simple solution.

I will try that. I am wondering why a new object would behave
differently (?)

-P

Well, one of the constructors for the UTF8Encoding class allows you to
include a BOM or not. If you use the default constructor then the
default is to not include the BOM.
 
P

pamela fluente

Well, one of the constructors for the UTF8Encoding class allows you to
include a BOM or not. If you use the default constructor then the
default is to not include the BOM.- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -

Thanks! That works fine.


-P
 

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