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

xmlvalid validates with libxml2, which reports it as Premature end of data in tag. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

Premature end of data in tag note line 1

Example

Reports the error
<note>
  <to>Ana</to>
Fixed
<note>
  <to>Ana</to>
</note>