Root element is missing

The input has no element at all.

The message, as .NET reports it
Root element is missing.

Why it happens

  • An empty file, string or response.
  • A Stream whose position is at its end: written to a MemoryStream and read back without Position = 0.

How to fix it

Check the length of the input; for a stream you have just written, set stream.Position = 0 before loading it.

.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 Document is empty. It checks the whole document in your browser and lists every schema error with its line and column, not only the first.

Document is empty

Example

Reports the error
(an empty file)
Fixed
<note/>