Latest web development tutorials

XML coding

XML documents can contain non-ASCII characters, such as Norwegian aa, or French eee.

To avoid errors, you need to specify the XML encoding, or XML file as Unicode.


XML encoding errors

If you load an XML document, you can get two different errors indicating encoding problems:

Invalid characters in the text content.

If your XML contains non-ASCII characters, and save the file as a single-byte encoding is not specified ANSI (or ASCII), you'll get an error.

Single-byte encoded XML file attributes .

Not the same as single-byte coded XML file attributes .

The current encoding is switched to the specified encoding is not supported

If your XML file is saved as a single byte encoding with the specified (WINDOWS-1252, ISO-8859-1, UTF-8) double-byte Unicode (or UTF-16), you'll get an error.

If your XML file is saved as a single byte with the specified ANSI-byte encoding (UTF-16) (or ASCII), you'll get an error.

No double-byte encoded XML files .

It has the same double-byte single-byte encoded XML files .


Windows Notepad

The default Windows Notepad will save the file as single-byte ANSI (ASCII).

If you select "Save As ...", you can specify the 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).

<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Norwegian: aa. French: eee</message>
</note>

Try to drag the file to your browser and see the results. Different browsers will show different results.

Different coding experience:

<?xml version="1.0" encoding="us-ascii"?>
<?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"?>

Try:

Save with the correct coding

Save with error code



in conclusion

  • Always use the encoding attribute
  • Supports coding editor
  • Make sure you know what encoding Editor
  • Using the same encoding in your coding properties