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.

Syntax:

<!-- Comments text -->

Here is XML single line comments example

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

Syntax:

<!-- multiple lines comments
line1
line2  -->

Here is XML multiple line comments example

<users>
    <user id="1">
    <!--
     Hello
     This is single user comments
     <name>john<name>
     <salary>5000</salary>
      -->
     </user>
</user>

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 - - >

<emps>
    <emp id="1">
    <!--
     Hi
     Multi-line comments in HTML tags
     <name>eric<name>
     <salary>4000</salary>
      -->
     </emp>
</emps>

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.
<![endif]-->

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
-->
<users>
    <user id="1">
        <name>john<name><!-- name of employee Inline comments -->
     <salary>5000</salary> <!-- salary of employee -->
     </user>
     <!-- department  -->
    <department>
        <name>sales<!-- sales department --><name><!-- department -->
     </department>
</user>

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.