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(xmlDeclara tion);
Method 2:
// insert the XML declaration
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration(" 1.0", "utf-8", "yes");
doc.InsertBefore(xmlDeclar ation, 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.
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("
doc.AppendChild(xmlDeclara
Method 2:
// insert the XML declaration
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("
doc.InsertBefore(xmlDeclar
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.
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");
Does the encoding specified in the declaration not do this? Do I need to do this explicitly using the XmlTextWriter?
doc.CreateXmlDeclaration("
By the way - you can still use the XmlDocument class. Just create the XmlTextWriter, then:
doc.WriteTo(tw);
Jim
doc.WriteTo(tw);
Jim
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
Problem solved! All I had to do was create my XmlDeclaration with null as the encoding as follows:
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("
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
Jim
XmlTextWriter tw = new XmlTextWriter(mSchemaNode.
Specifying the UTF8 encoding removes the identifiers.
Jim