Garbage characters in .Net XmlDocument whan adding XmlDeclaration

I am creating an XML document (Visual Studio 2005 using .Net 2.0) that will be read by a Java program using the JDom XML library.

I create my XML document and add an XmlDeclaration element then save to a file. Looking at the file in a hex editor I see 3 extra bytes inserted at the very beginning of the file (EF BB BF) before the beginning of the declaration. Parsing of this file by the JDom library fails because of the rubbish characters.

These garbage characters do NOT appear if I don't add the declaration header to the document.

I have tried a couple of methods for adding the header with exactly the same result:

Method 1:
            // insert the XML declaration
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            doc.AppendChild(xmlDeclaration);

Method 2:
            // insert the XML declaration
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            doc.InsertBefore(xmlDeclaration, doc.DocumentElement);

I write the XML file using doc.Save(path). The start of the file then looks like:
             ï»¿<?xml version

My only solution at the moment is to leave the declaration off. This is an urgent problem so I am assigning good points to it ....

Thanks in advance for your help.
CSB00001Asked:
Who is Participating?
 
JimBrandleyCommented:
If they are there, and that's the way you defined it, then apparently it ignored the setting. I always just use the XmlTextWriter because is's fast. Have not tried saving with the document.

Jim
0
 
JimBrandleyCommented:
These are the bytes that identify the encoding. If you do not want them there, use:
XmlTextWriter tw = new XmlTextWriter(mSchemaNode.Path, System.Text.Encoding.UTF8);

Specifying the UTF8 encoding removes the identifiers.

Jim
0
 
CSB00001Author Commented:
Hi Jim,

Does the encoding specified in the declaration not do this?  Do I need to do this explicitly using the XmlTextWriter?

  doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
JimBrandleyCommented:
By the way - you can still use the XmlDocument class. Just create the XmlTextWriter, then:
doc.WriteTo(tw);

Jim
0
 
CSB00001Author Commented:
Thanks Jim,

Problem solved! All I had to do was create my XmlDeclaration with null as the encoding as follows:

XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", null, "yes");

Then, when I write it out using doc.Save() the encoding identifiers are not written. The same thing applies if I use the text writer.

Thanks a heap!
0
 
JimBrandleyCommented:
My pleasure. Good luck!

Jim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.