</>
ValidateHTML

Invalid or Missing XML Declaration

The XML declaration (<?xml version="1.0" encoding="UTF-8"?>) must appear at the very beginning of the document with no whitespace or BOM before it. The version attribute is required and must come first. The encoding attribute is optional but recommended. Common mistakes include putting the declaration on line 2, using wrong attribute order, misspelling attributes, or adding extra whitespace before the <?.

Why It Matters

An invalid XML declaration can cause parsers to misinterpret the entire document encoding, leading to garbled text or complete parse failure. Some parsers are lenient, but strict XML processors reject the document outright.

Code Examples

Invalid XML

<?xml encoding="UTF-8" version="1.0"?>
<root>
  <message>Hello World</message>
</root>
Valid XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <message>Hello World</message>
</root>

How to Fix

  • 1Place the XML declaration on the very first line with no whitespace, BOM, or blank lines before it.
  • 2Always put the version attribute first: <?xml version="1.0" ...?>.
  • 3The encoding attribute must come after version: <?xml version="1.0" encoding="UTF-8"?>.
  • 4Use UTF-8 encoding unless you have a specific reason to use another encoding.

Check Your XML Now

Our XML validator detects this error automatically and shows the exact line and column.

Open XML Validator
Recommended

Hostinger Fast & Affordable Web Hosting

Serve well-formed XML feeds and APIs on reliable hosting.

Get 80% Off Hosting →

Related XML Errors

View all XML errors