# Reference to undeclared entity

Source: https://xmlvalid.com/errors/reference-to-undeclared-entity

> The document uses a named entity, like &amp;nbsp;, that XML does not define.

The message, as .NET reports it: `Reference to undeclared entity 'nbsp'. Line 1, position 11.`

## Why it happens

- HTML entities in XML: `&nbsp;`, `&copy;`. XML defines only five: `&lt;`, `&gt;`, `&amp;`, `&quot;`, `&apos;`.
- An entity declared in a DTD that is not processed (`DtdProcessing.Ignore` or `Prohibit`).

## How to fix it

- Replace the entity with the character itself or a numeric reference (`&#160;`). Converting HTML text before putting it into XML avoids it.
- .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 [Entity not defined](https://xmlvalid.com/errors/entity-not-defined): `Entity 'nbsp' not defined`

## Example that fails

```xml
<p>Price:&nbsp;10</p>
```

## Fixed

```xml
<p>Price:&#160;10</p>
```
