# Unexpected end of file has occurred

Source: https://xmlvalid.com/errors/unexpected-end-of-file-has-occurred

> The document ends while elements are still open.

The message, as .NET reports it: `Unexpected end of file has occurred. The following elements are not closed: note. Line 3, position 1.`

## Why it happens

- A truncated file or stream: an interrupted download, an `XmlWriter` that was not flushed or disposed before the output was read.
- A missing end tag for the elements the message lists.

## How to fix it

- Dispose (or `Flush()`) the `XmlWriter` before using its output, and check that the file is complete. Then close the elements the message lists.
- .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 [Premature end of data in tag](https://xmlvalid.com/errors/premature-end-of-data): `Premature end of data in tag note line 1`

## Example that fails

```xml
<note>
  <to>Ana</to>
```

## Fixed

```xml
<note>
  <to>Ana</to>
</note>
```
