Home All Groups Group Topic Archive Search About

Dirty chars with save method

Author
5 Nov 2007 9:29 AM
Laura Villa
Hello,
I'm trying to create an xml doc using the XMLDocument object (Framework 1.1)
to export data to an application. The file is created using this syntax:

string newDocumentText = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
xmlDoc = new XmlDocument();
xmlDoc.LoadXml(newDocumentText);

After that, I append several nodes to the document using the AppendChild
method and create the file with the XMLDocument Save Method.

xmlDoc.Save(FileName);

Everything seems ok but, if I try to open the file with a byte editor, I
noticed that there are some dirty chars at the beginning of the file:

<?xml version="1.0" encoding="UTF-8"?>
....

These chars are not visible within Internet Explorer, notepad, or XMLSpy.
But the destination application crashes because of them.

I tried to save the file using the overloads of the save method, but the
results are worse.

Any hint? Is there a way to create a clean xml file?

Thanks a lot,
Laura

Author
5 Nov 2007 1:03 PM
Martin Honnen
Laura Villa wrote:

> Everything seems ok but, if I try to open the file with a byte editor, I
> noticed that there are some dirty chars at the beginning of the file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> ...

That is a BOM (byte order mark). If you don't want that for UTF-8 you
can use new UTF8Encoding(false) to create the encoding and save using
that e.g.
   xmlDocumentInstance.Save(new StreamWriter(File.Open("file.xml",
FileMode.Create), new UTF8Encoding(false)));

--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

AddThis Social Bookmark Button