# The element has invalid child element

Source: https://xmlvalid.com/errors/element-has-invalid-child-element

> .NET found a child element the schema does not allow at that point: out of order, misspelt, or in another namespace.

The message, as .NET reports it: `The element 'order' has invalid child element 'qty'. List of possible elements expected: 'id'.`

## Why it happens

- Elements in a different order than the schema's `xs:sequence`: the order matters.
- A namespace mismatch, when the names look identical. .NET writes a namespaced name as `'id' in namespace 'urn:example'`: if the expected element has a namespace and the one in the document has none (a missing `xmlns="…"`), they don't match.
- An element the schema doesn't allow there at all. Without a *List of possible elements expected*, nothing more is allowed in the parent at that point.

## How to fix it

- The list names what the schema accepts at that point: move the element there, add the one that is missing before it, or compare namespaces. With `XmlSerializer`, check the `Namespace` of `[XmlRoot]` and `[XmlElement]` and the `Order` of the properties.
- .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 [This element is not expected](https://xmlvalid.com/errors/this-element-is-not-expected): `Element 'qty': This element is not expected. Expected is ( id ).`

## Example that fails

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

## Fixed

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