By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

C# and XmlTextWriter sends some garbage.

P: n/a
Hi,

My XML files created with XmlTextWriter routines has been resulting in
working files, but they include three bytes of garbage at the start of
the file.

The bytes are: $EF $BB $BF (I think it always outputs these)

I am using VS 2002 and C#.

My coding involves opening a file stream, then the XmlTextWriter...

------CODE FRAGMENT------------------
string xmlpath = Server.MapPath("../xmldata");

myXmlFile = System.IO.File.Create(xmlpath + "\\" + "myxmlfile.xml");
if (myXmlFile.CanWrite)
{
myXmlFile.Lock(0,4000);
myXmlFile.Flush(); // added to try a workaround for the 'bug'
myXmlFile.Position = 0; // reposition to start, workaround.
myXml = new XmlTextWriter(myXmlFile, System.Text.Encoding.UTF8);
myXml.Formatting = System.Xml.Formatting.Indented;
myXml.Indentation = 1;
myXml.IndentChar = (char) 9;
myXml.WriteStartDocument();
// _XmlNL();
myXml.WriteStartElement("Board");
----END FRAGMENT---------------------------

Actually, the files with the garbage display fine as raw files in IE on
Windows and in FireFox on Mac, but IE on Mac chokes on the file, giving
an error about the garbage.

Anybody know if this is a bug in the C# routines in .NET 1.1, or is it
just me?

Bob Dickow
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Robert Dickow" <di***********@uidaho.edu> wrote in message
news:Xn*****************************@38.119.71.210 ...
Hi,

My XML files created with XmlTextWriter routines has been resulting in
working files, but they include three bytes of garbage at the start of
the file.

The bytes are: $EF $BB $BF (I think it always outputs these)

I am using VS 2002 and C#.
<SNIP>
Anybody know if this is a bug in the C# routines in .NET 1.1, or is it
just me?

Bob Dickow


Bob,

I cannot say this is a bug, but I can verify that in my files that are
generated using VS 2003 and .Net 1.1 I have the same bytes at the beginning
of my files.
Nov 16 '05 #2

P: n/a
Here I go answering my own question...

It looks like the XmlTextWriter will sometimes have garbage in its
buffer, so....

.........
myXml.Flush(); // do this before any output to xml file.

myXmlFileStreamHandle.Position = 0 // set file pointer to beginning.

.... all is ok now.

Robert Dickow <di***********@uidaho.edu> wrote in
news:Xn*****************************@38.119.71.210 :
<snip>
My XML files created with XmlTextWriter routines has been resulting in
working files, but they include three bytes of garbage at the start of
the file.

The bytes are: $EF $BB $BF (I think it always outputs these)

<snip>
if (myXmlFile.CanWrite)
{
myXmlFile.Lock(0,4000);
myXmlFile.Flush(); // added to try a workaround for the 'bug'
myXmlFile.Position = 0; // reposition to start, workaround.
myXml = new XmlTextWriter(myXmlFile, System.Text.Encoding.UTF8);
myXml.Formatting = System.Xml.Formatting.Indented;
myXml.Indentation = 1;
myXml.IndentChar = (char) 9;
myXml.WriteStartDocument();
<snip>

Nov 16 '05 #3

P: n/a
Those three bytes represent the preamble used to indicate that the file is incoded in UTF8. The default UTF8 encoder accessed
through Text.Encoding.UTF8 outputs that preamble. To prevent those three bytes then create your own instance of UTF8Encoding.
The default for a new instance is to not emit a preamble set of bytes.

Kelly

"Robert Dickow" <di***********@uidaho.edu> wrote in message news:Xn*****************************@38.119.71.210 ...
Hi,

My XML files created with XmlTextWriter routines has been resulting in
working files, but they include three bytes of garbage at the start of
the file.

The bytes are: $EF $BB $BF (I think it always outputs these)

I am using VS 2002 and C#.

My coding involves opening a file stream, then the XmlTextWriter...

------CODE FRAGMENT------------------
string xmlpath = Server.MapPath("../xmldata");

myXmlFile = System.IO.File.Create(xmlpath + "\\" + "myxmlfile.xml");
if (myXmlFile.CanWrite)
{
myXmlFile.Lock(0,4000);
myXmlFile.Flush(); // added to try a workaround for the 'bug'
myXmlFile.Position = 0; // reposition to start, workaround.
myXml = new XmlTextWriter(myXmlFile, System.Text.Encoding.UTF8);
myXml.Formatting = System.Xml.Formatting.Indented;
myXml.Indentation = 1;
myXml.IndentChar = (char) 9;
myXml.WriteStartDocument();
// _XmlNL();
myXml.WriteStartElement("Board");
----END FRAGMENT---------------------------

Actually, the files with the garbage display fine as raw files in IE on
Windows and in FireFox on Mac, but IE on Mac chokes on the file, giving
an error about the garbage.

Anybody know if this is a bug in the C# routines in .NET 1.1, or is it
just me?

Bob Dickow

Nov 16 '05 #4

P: n/a
Robert Dickow <di***********@uidaho.edu> wrote:
My XML files created with XmlTextWriter routines has been resulting in
working files, but they include three bytes of garbage at the start of
the file.
They're not garbage. They're the UTF-8 encoded byte-ordering mark.

<snip>
Actually, the files with the garbage display fine as raw files in IE on
Windows and in FireFox on Mac, but IE on Mac chokes on the file, giving
an error about the garbage.

Anybody know if this is a bug in the C# routines in .NET 1.1, or is it
just me?


It sounds like it's a bug in IE on the Mac.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
Hey,
I usually start my XML files with:
<?xml version="1.0" encoding="windows-1251" ?>
or whatever encoding you want to use...
(I sometimes want to "show" something in Russian,
and I ran into some compatibility issues with
unicode, so I just use the encoding I want to use.)

I hope this helps.
Good luck.

On 26 Dec 2004 16:06:23 -0600, Robert Dickow <di***********@uidaho.edu>
wrote:
Hi,

My XML files created with XmlTextWriter routines has been resulting in
working files, but they include three bytes of garbage at the start of
the file.

The bytes are: $EF $BB $BF (I think it always outputs these)

I am using VS 2002 and C#.

My coding involves opening a file stream, then the XmlTextWriter...

------CODE FRAGMENT------------------
string xmlpath = Server.MapPath("../xmldata");

myXmlFile = System.IO.File.Create(xmlpath + "\\" + "myxmlfile.xml");
if (myXmlFile.CanWrite)
{
myXmlFile.Lock(0,4000);
myXmlFile.Flush(); // added to try a workaround for the 'bug'
myXmlFile.Position = 0; // reposition to start, workaround.
myXml = new XmlTextWriter(myXmlFile, System.Text.Encoding.UTF8);
myXml.Formatting = System.Xml.Formatting.Indented;
myXml.Indentation = 1;
myXml.IndentChar = (char) 9;
myXml.WriteStartDocument();
// _XmlNL();
myXml.WriteStartElement("Board");
----END FRAGMENT---------------------------

Actually, the files with the garbage display fine as raw files in IE on
Windows and in FireFox on Mac, but IE on Mac chokes on the file, giving
an error about the garbage.

Anybody know if this is a bug in the C# routines in .NET 1.1, or is it
just me?

Bob Dickow


--
Regards,
Nurchi BECHED
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.