How to Comment out a Line in XML

By Elizabeth Mott

XML provides a markup language for storing data and conveying it from one location to another. Like HTML, it uses plain text you can edit in any application that writes TXT files. Unlike HTML, XML tags provide descriptive detail about content rather than visual instructions for displaying it. As you define XML tags to characterize a set of information, you can use comments to serve two purposes within your work: to explain it, and to omit parts of it from the code that other applications process. If you already know HTML comment syntax, you also know the XML equivalent.

Single-Line Comment

Step 1

Insert your cursor at the beginning of a line you want to comment out. Type a less-than symbol followed by an exclamation point and two dashes.

Step 2

Move your cursor to the end of the line. Type two dashes followed by a greater-than symbol. If your line consists of the statement "Use this section to build a description of adoptable cats," it looks like this with both comment tags in place:

Step 3

Read your comment to verify that it doesn't contain any instances of two consecutive dashes other than in its opening and closing comment tags. Consecutive dashes trigger a false end-of-comment signal that disrupts your syntax.

Commented Section

Step 1

Insert your cursor on a blank line above the section of XML code you want to comment out. Type a less-than symbol followed by an exclamation point and two dashes.

Step 2

Move your cursor to a blank line after the section of XML code. Type two dashes followed by a greater-than symbol. With comments in place, a section of XML code looks like this:

Step 3

Read your commented section to verify that it doesn't contain a comment within the lines you commented out. Nested comments constitute an error in XML.

Tips & Warnings

  • XML doesn't allow for the use of the less-than character within its code, except as part of an opening comment tag. To include the character in anything except a comment tag, enter it as an entity reference, a coded sequence that turns into a specific character when an application parses your XML. Every entity reference begins with an ampersand and ends with a semicolon. Between those two characters, the entity reference for the less-than symbol contains the lower-case letters "lt," without the quotation marks and punctuation. The greater-than symbol doesn't constitute an illegal character in XML, but it's good practice to use its entity reference as well, which includes the letters "gt."
  • If you fail to open and close your comments correctly, or place them around a section of code that contains an ID, your XML will not parse properly.