XML Encoding
XML documents can contain non-ASCII characters, such as Norwegian æ ø å, or French ê è é.
To avoid errors, you need to specify the XML encoding, or save the XML file as Unicode.
XML Encoding Errors
If you load an XML document, you can get two different errors indicating encoding problems:
Invalid character found in text content.
If your XML contains non-ASCII characters and the file is saved as single-byte ANSI (or ASCII) without a specified encoding, you will get an error.
XML file with single-byte encoding attribute。
The same single-byte XML file without encoding attribute。
Switch current encoding to a specified encoding that is not supported.
If your XML file is saved as double-byte Unicode (or UTF-16) with a specified single-byte encoding (WINDOWS-1252, ISO-8859-1, UTF-8), you will get an error.
If your XML file is saved as single-byte ANSI (or ASCII) with a specified double-byte encoding (UTF-16), you will also get an error.
Double-byte XML file without encoding。
The same double-byte XML file with single-byte encoding。
Windows Notepad
Windows Notepad saves files as single-byte ANSI (ASCII) by default.
If you choose "Save As...", you can specify ANSI, UTF-8, Unicode (UTF-16), or Unicode Big.
Save the following XML as ANSI, UTF-8, and Unicode (note that the document does not contain any encoding attribute).
<note>
<from>Jani</from>
<to>Tove</to>
<message>Norwegian: æøå. French: êèé</message>
</note>
Try dragging the file into your browser and view the result. Different browsers will display different results.
Experience with different encodings:
<?xml version="1.0" encoding="windows-1252"?>
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-16"?>
Please try:
Saving with incorrect encoding
Conclusion
- Always use the encoding attribute
- Use an editor that supports encoding
- Make sure you know what encoding your editor uses
- Use the same encoding in your encoding attribute