XML Encoding
XML documents can contain non-ASCII characters, such as the Norwegian æ ø å, or the 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 may get two different errors indicating an encoding problem:
Invalid character in the text content.
If your XML contains non-ASCII characters and the file is saved as single-byte ANSI (or ASCII) without specifying an encoding, you will get an error.
An XML File with a Single-Byte Encoding Attribute。
The Same Single-Byte XML File Without an Encoding Attribute。
Switch the current encoding to an unsupported specified encoding
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 an encoding。
The same double-byte XML file with a 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 XML below as ANSI, UTF-8, and Unicode (note that the document does not contain any encoding attribute).
Try dragging the file into your browser and see the result. Different browsers will display different results.
Experiences with different encodings:
Try it:
Saved with the correct encoding
Conclusion
- Always use an 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