# Invalid character in the given encoding

Source: https://xmlvalid.com/errors/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

libxml2, which xmlvalid validates with, reports it as [Invalid bytes in character encoding](https://xmlvalid.com/errors/invalid-bytes-in-character-encoding): `Invalid bytes in character encoding`

## Example that fails

```xml
<note>café</note>
```

## Fixed

```xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>café</note>
```
