?
Solved

Garbage characters in .Net XmlDocument whan adding XmlDeclaration

Posted on 2007-09-29
6
Medium Priority
?
644 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:CSB00001
  • 4
  • 2
6 Comments
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19985381
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
 

Author Comment

by:CSB00001
ID: 19985397
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
 
LVL 22

Expert Comment

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

Jim
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 19985439
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
 

Author Comment

by:CSB00001
ID: 19985450
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
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19985453
My pleasure. Good luck!

Jim
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question