Introduction to XML | XML Basics | XML Comments

XML documents can contain comments. This article explains how to write them.

Writing Comments

Use the following syntax to add a note to an XML document.

<!-- comment -->

A comment begins with <!-- and ends with -->. Write the comment text between these markers.

Comments can be written in many places within an XML document.

<?xml version="1.0" encoding="UTF-8" ?>
<foods>
  <food>
    <!-- Check required inventory -->
    <name>banana</name>
    <color>yellow</color>
  </food>

  <food>
    <name>apple</name>
    <!-- Red with a green tint -->
    <color>red</color>
  </food>
</foods>

Writing comments

However, do not write a comment before the XML declaration or inside a start or end tag.

<!-- groceries -->
<?xml version="1.0" encoding="UTF-8" ?>
<foods>
  <food>
    <name>banana</name>
  </food>
</foods>

Comment before an XML declaration

A comment must not contain --.

<?xml version="1.0" encoding="UTF-8" ?>
<foods>
  <food>
    <!-- banana -- domestic -->
    <name>banana</name>
  </food>
</foods>

Invalid XML comment

This page explained how to write comments in XML documents.