How to Comment out a Line in XML

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Unlike HTML, XML tags provide descriptive detail about content rather than visual instructions for displaying it.
Image Credit: RossHelen/iStock/GettyImages

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.

Advertisement

Single-Line Comment in XML

Video of the Day

If you want to comment out a single line in the XML code, 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. Move your cursor to the end of the line and then type two dashes followed by a greater-than symbol.

Advertisement

Video of the Day

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:

You'll want to 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.

Advertisement

Section Comment in XML

Rather than just commenting out a single line, you may need to comment out a whole section of your XML code. To do this, insert your cursor on a blank line above the section of XML code you want to comment out and then type a less-than symbol followed by an exclamation point and two dashes.

Advertisement

To end the comment, move your cursor down to a blank line after the section of XML code you are commenting out. Type two dashes followed by a greater-than symbol. With comments in place, a section of XML code looks like this:

Advertisement

Advertisement

Again you'll want to 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.

XML Syntax Rules and Comments

When making comments, remember a few basic XML syntax rules. 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.

Advertisement

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

Advertisement

Also, know that 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.

Advertisement

Advertisement

references & resources

Report an Issue

screenshot of the current page

Screenshot loading...