Document Structure
Every XML document follows the same basic skeleton: an optional declaration line at the very top, followed by exactly one element that wraps everything else in the document.
The XML declaration
Most XML documents open with a declaration stating the XML version and character encoding in use:
<?xml version="1.0" encoding="UTF-8"?>
This line is technically optional — a parser will assume XML 1.0 and UTF-8 if it's missing — but it's considered good practice to include it explicitly. If you do include it, it has to be the very first thing in the file, with nothing (not even a blank line) before it.
Exactly one root element
Everything else in the document has to sit inside a single top-level element, usually called the root element. A document with two elements sitting side by side at the top level is not valid XML, even though each one individually looks fine:
<?xml version="1.0" encoding="UTF-8"?> <library> <book>Dune</book> <book>Foundation</book> </library>
<?xml version="1.0" encoding="UTF-8"?> <book>Dune</book> <book>Foundation</book>
error: XML document structures must start and end within the same entity. (extra content at the end of the document)
The second example has two <book> elements sitting at the top level with nothing wrapping them — a parser reads the first <book>...</book> as the complete document and then finds unexpected content immediately after it, which it treats as an error rather than a second top-level element.
Nesting inside the root
Once you have a root element, everything else nests inside it to whatever depth the data actually needs — a root can contain children, and those children can contain their own children:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>Dune</title>
<author>
<name>Frank Herbert</name>
<nationality>American</nationality>
</author>
</book>
</library>