How to Comment in XML?

How to write Comment in XML file/ code?

XML Comment Line Syntax

XML is the abbreviation for EXtensible Markup Language. It is designed to store and transport data and is designed as both human and machine readable format.

XML comments are similar to HTML comments. The comments are added as notes for understanding the XML code. XML comments briefs about the code and help to edit the source code at a later time. XML comments are visible only in the source code and not in the XML code. Learn how to comment in XML and simple comment line syntax for single and multi line XML comments.
How To Comment In Xml
An XML comment starts with a preceding "lesser than arrow" symbol followed by an exclamatory mark and two dashes (<!--) and ends with two dashes followed by a "greater than arrow" symbol (-->). Multiple lines or blocks of XML code can also be included within the <!-- and -->.

XML Comment: Syntax
The syntax for writing comments in XML is similar to that of HTML.

<!-- This is a single line XML Comment -->
<!-- This is a
multi-line
XML comment -->
Xml Comment Line Syntax
Example 1:

<note>
<to>Vel</to> <!-- This is the receiver -->
<from>Raj</from> <!-- This is the sender -->
<heading>Weekend Plan</heading>
<body>Reminder for our weekend plan</body>
</note>
Xml Comments Example
Example 2: Commenting a single tag
In the below example the <heading> tag is commented along with the code within it.
<note>
<to>Vel</to>
<from>Raj</from>
<!-- heading -->Weekend Plan</heading>
<body>Reminder for our weekend plan</body>
</note>
Xml Comment Line Syntax Examples
Example 3: Commenting multiple tags
In the below example, a block of XML code including multiple tags has been commented along with the code within it.
Xml Comments Examples
XML Comments: Rules to Remember
→ Comments cannot appear before XML declaration.
→ Comments can appear anywhere in XML code.
→ Comments cannot appear within attribute values.
→ Comments cannot be nested inside the other comments.
→ Two hyphens in the middle of a comment is not allowed.
→ Two hyphens in the middle of a comment, separated by a space is allowed.
→ Single hyphen in the middle of a comment is allowed.

Xmlcomment Restrictions


Related Topics