# The element cannot contain text

Source: https://xmlvalid.com/errors/element-cannot-contain-text

> Text appears directly inside an element that may only contain other elements.

The message, as .NET reports it: `The element 'order' cannot contain text. List of possible elements expected: 'id'.`

## Why it happens

- Stray text between child elements, or a value written into the parent instead of its child element.
- A non-breaking space or other character that looks like whitespace but isn't.

## How to fix it

- Move the text into the right child element or delete it. Spaces and line breaks between elements are fine.
- .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 [Character content other than whitespace is not allowed](https://xmlvalid.com/errors/character-content-element-only): `Element 'order': Character content other than whitespace is not allowed because the content type is 'element-only'.`

## Example that fails

```xml
<order currency="EUR">hello
  <id>A-1</id>
  <qty>1</qty>
</order>
```

## Fixed

```xml
<order currency="EUR">
  <id>A-1</id>
  <qty>1</qty>
  <note>hello</note>
</order>
```
