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
<?xml encoding="UTF-8" version="1.0"?> <root> <message>Hello World</message> </root>
<?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 ValidatorHostinger — Fast & Affordable Web Hosting
Serve well-formed XML feeds and APIs on reliable hosting.
Related XML Errors
Multiple Root Elements in XML
XML requires exactly one root element. Learn how to fix the multiple root elements error.
Unclosed Tag in XML
Learn why XML throws an unclosed tag error and how to fix it. Every opening tag must have a matching closing tag or be self-closing.
Unescaped Special Characters in XML
Characters like &, <, and > must be escaped in XML text content. Learn how to fix unescaped character errors.