Link to home
Start Free TrialLog in
Avatar of CSB00001
CSB00001

asked on

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.
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

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
Avatar of CSB00001
CSB00001

ASKER

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");
By the way - you can still use the XmlDocument class. Just create the XmlTextWriter, then:
doc.WriteTo(tw);

Jim
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
My pleasure. Good luck!

Jim