How do I avoid writing the Byte Order Mark ?

  • Thread starter Thread starter pamela fluente
  • Start date Start date
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
 
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.
 
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
 
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).
 
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
 
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
 
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.
 
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
 
Back
Top