In this tutorial, You learn below things writing XML comments. Comments are human-readable text, that helps users to understand code better, Compilers in Programming languages ignore the comment.

In XML, Comments

XML Comments

the developer writes Comments to describe the human-readable text or tag in XML, which helps users to understand the XML better.

processor or parser ignores comments during XML parse.

Comments start with (less than, exclamation symbol and two dashes) <!-- and end with (two dashes followed by greater than symbol)-->.

Comments can be single or multi-line comments.

  • Single line comments:

Comments text is placed in a single line.


<!-- Comments text -->

Here is XML single line comments example

    <user id="1">
    <!-- Hello This is single user comments -->
  • Multi-line comments: Comment text placed in multiple lines wrapped in comment syntax. Comment text can have string text or tags.


<!-- multiple lines comments
line2  -->

Here is XML multiple line comments example

    <user id="1">
     This is single user comments

How do I comment out a block of tags in XML?

It is easy to comment Block of tags with multi-line syntax.

Multi-line comments starts with <!-- followed by comments, end with - - >

    <emp id="1">
     Multi-line comments in HTML tags

Conditional XML Comments

Conditional comments are not part of the original XML Spec. XHTML spec supports Conditional XML comments.

It helps browser to understand the content better

<!--[if Encoding is UTF-8]>
   This content Supports UTF-8 Encoding.

How to write Comments in Empty Tags

Empty tags have no value in the beginning and end tags, However, You can place single or multi-line comments.

<norecords><!-- empty tag comment --></norecords>

XML Comments example:

Following is an example

    XML document for a list of users
    Each user has an information id attribute, name, and salary
    <user id="1">
        <name>john<name><!-- name of employee Inline comments -->
     <salary>5000</salary> <!-- salary of employee -->
     <!-- department  -->
        <name>sales<!-- sales department --><name><!-- department -->

XML Comments do’s and don’t’s conventions.

  • Can write in the content
  • Not allowed in XML attributes
  • Allowed inside a tag
  • nested comments are not allowed
  • Comments help readability and maintainability
  • XML editors provide an inbuilt feature to comment and uncomment a code block.
  • Comments text should not contain --, as treat this as the end of a comment.