Control characters appearing in XDocument.Save(), but visible to Unix only?

306 views Asked by At

I am saving a simple XDocument using

xDoc.Save(result.Filename, SaveOptions.OmitDuplicateNamespaces);

When we open the file and process it on a Windows system (Server2008 or Win7) the xml is correct and processes in the next system correctly.

Howevever, when we move to our production system (unix) the xml fails to read correctly.

When we vi the resulting file on the unix box there are three leading control characters. Remove these and the xml processes correctly.

I cannot see these characters in Notepad++ (which shows the file as ANSI/UTF8).

Has anyone any idea what these characters ARE, how they get there...and how to either remove them or stop them appearing?

2

There are 2 answers

2
Mike Chaliy On BEST ANSWER

You can omit Unicode BOM, just pass new UTF8Encoding(false) to the save method.

Something like this (please treat it as pseudo code):

using(var writer =  new StreamWriter(result.Filename, false, new UTF8Encoding(false))){
    xDoc.Save(writer , SaveOptions.OmitDuplicateNamespaces);
}
0
leppie On

This the Unicode BOM (byte-order mark).

If it is showing in Linux, get a Unicode capable editor/viewer.