# Is an undeclared prefix

Source: https://xmlvalid.com/errors/is-an-undeclared-prefix

> An element or attribute uses a namespace prefix that no xmlns declaration defines.

The message, as .NET reports it: `'inv' is an undeclared prefix. Line 1, position 2.`

## Why it happens

- A missing or misplaced `xmlns:prefix="…"` declaration.
- A fragment taken out of a larger document without the namespace declarations of its ancestors.

## How to fix it

- Declare the prefix on the element or an ancestor, usually the root, with the namespace the format defines. When extracting a fragment, write it with `XElement` or `XmlWriter`, which add the declarations it needs.
- .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 [Namespace prefix is not defined](https://xmlvalid.com/errors/namespace-prefix-is-not-defined): `Namespace prefix inv on invoice is not defined`

## Example that fails

```xml
<inv:invoice/>
```

## Fixed

```xml
<inv:invoice xmlns:inv="urn:example:invoice"/>
```
