XML
Coding
Commenting Out
Programming
Code Blocks

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To comment out a block of XML, wrap it in the standard XML comment syntax: <!-- at the start and --> at the end. That is the direct answer, but XML comments have stricter rules than many developers expect.

The two big limitations are that XML comments cannot be nested and the text inside the comment cannot contain the sequence --. Those rules are what usually break block-comment attempts.

Basic XML Comment Syntax

A single XML comment looks like this:

xml
<!-- This is a comment -->

A block comment uses the same syntax across multiple lines:

xml
1<root>
2    <!--
3    <feature enabled="true">
4        <name>beta</name>
5    </feature>
6    -->
7</root>

Everything inside the comment is ignored by the XML parser.

Commenting Out a Block of Tags

If the block contains no XML comments of its own and no invalid comment content, you can comment it out directly:

xml
1<settings>
2    <!--
3    <cache enabled="true" />
4    <timeout>30</timeout>
5    <retries>3</retries>
6    -->
7</settings>

This is the usual way to disable a block temporarily while testing configuration changes or editing a document.

The Biggest Limitation: No Nested Comments

This fails if the block you are trying to comment out already contains an XML comment:

xml
1<settings>
2    <!--
3    <cache enabled="true" />
4    <!-- existing comment -->
5    <timeout>30</timeout>
6    -->
7</settings>

That is invalid XML because XML comments cannot be nested.

In that situation, you have to choose a different approach:

  • remove or rewrite the inner comment first
  • comment out smaller sections individually
  • temporarily delete the block and restore it from version control later

Another Limitation: No Double Hyphen Inside Comments

The text inside an XML comment cannot contain --. For example, this is invalid:

xml
<!-- invalid -- comment -->

This rule matters when you comment out content that already contains strings with repeated hyphens. If such text appears inside the block, the whole document becomes invalid until you change that content.

A Safer Editing Workflow

If you are unsure whether the block contains nested comments or forbidden -- sequences, comment it out in stages rather than in one huge block.

For example, instead of wrapping the whole parent element immediately, you can comment out one child element at a time:

xml
1<settings>
2    <!-- <cache enabled="true" /> -->
3    <!-- <timeout>30</timeout> -->
4    <!-- <retries>3</retries> -->
5</settings>

This is more verbose, but it avoids some of the parser errors that appear when a large XML fragment has existing comments inside it.

Comments Versus Data Removal

Remember that comments are a source-level editing tool. They are useful when a human needs to disable a block temporarily, but they are not a feature-toggle system and not a general-purpose runtime mechanism.

If the XML document is part of application configuration, it is often better to use a dedicated attribute or element that explicitly disables behavior rather than relying on comments.

For example, this is easier for software to reason about than a removed block hidden inside comments:

xml
<feature enabled="false">
    <name>beta</name>
</feature>

That is not always possible, but it is worth considering for long-term maintainability.

Common Pitfalls

The biggest pitfall is trying to nest comments. XML does not allow it, so a block that already contains <!-- ... --> cannot simply be wrapped in another comment.

Another common issue is forgetting about the -- rule. Even if the inner text is not itself a comment, a double hyphen inside the commented block still breaks XML validity.

People also assume that comment syntax in XML behaves like it does in HTML editors or programming languages. XML parsers are stricter, so documents that look visually fine can still be invalid.

Finally, do not leave large commented-out blocks in important configuration files forever. They make the document harder to read and are usually better handled by version control.

Summary

  • To comment out a block in XML, wrap it with <!-- and -->.
  • XML comments cannot be nested.
  • The text inside an XML comment cannot contain --.
  • If a large block will not comment cleanly, comment smaller parts or remove inner comments first.
  • Use comments for temporary editing, not as a long-term substitute for proper configuration structure.

Course illustration
Course illustration

All Rights Reserved.