Invalid character in the given encoding

The bytes are not valid in the encoding the document is read with.

The message, as .NET reports it
Invalid character in the given encoding. Line 1, position 10.

Why it happens

  • A file saved as Windows-1252 or ISO-8859-1 without an encoding in its declaration, so it is read as UTF-8.
  • A declaration that names a different encoding than the file was saved in.

How to fix it

Make the declaration match the bytes: save as UTF-8, or declare the real encoding. On .NET Core and later, Windows code pages need Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) before such a file can be read.

.NET throws an XmlSchemaValidationException at the first schema error unless XmlReaderSettings.ValidationEventHandler is set, which then receives every one with its line and position. A well-formedness error is an XmlException and ends reading whatever the settings.

The same problem in xmlvalid

xmlvalid validates with libxml2, which reports it as Invalid bytes in character encoding. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

Invalid bytes in character encoding

Example

Reports the error
<note>café</note>
Fixed
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>café</note>