138

This rather is a verification just not to miss out.

Is/n't there a line-comment in XML? So, one without a closer, like "//" the compiler uses.

I saw How do I comment out a block of tags in XML? and several other discussions.

This type of comment would be practical to comment out a line without bothering its closer somewhere down.

2
  • 2
    XML is designed on the assumption that tools like editors will often perform word-wrapping, and that line endings are therefore fragile. It would be a bad idea if line endings had to be preserved just in case of comments. Jun 27, 2013 at 7:23
  • 2
    Surely editors only wrap lines for display. I've never known hard newlines to be added arbitrarily to the file itself. It's an infernal nuisance not having a line comment.
    – John White
    Jan 3, 2014 at 8:51

5 Answers 5

163

No, there is no way to comment a line in XML and have the comment end automatically on a linebreak.

XML has only one definition for a comment:

'<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'

XML forbids -- in comments to maintain compatibility with SGML.

1
  • A workaround is to use line comments (//) and run a script to strip out those comments before use (can be part of a singe-step build process). It makes for a much more convenient way of using comments. (I have used this technique with WiX source (installer tool for Windows). A number of other steps were required for building the installer, so this was just one more step to add to a build script/process.) Dec 16, 2019 at 22:30
21

As others have said, there is no way to do a single line comment legally in XML that comments out multiple lines, but, there are ways to make commenting out segments of XML easier.

Looking at the example below, if you add '>' to line one, the XmlTag will be uncommented. Remove the '>' and it's commented out again. This is the simplest way that I've seen to quickly comment/uncomment XML without breaking things.

<!-- --
<XmlTag variable="0" />
<!-- -->

The added benefit is that you only manipulate the top comment, and the bottom comment can just sit there forever. This breaks compatibility with SGML and some XML parsers will barf on it. So long as this isn't a permanent fixture in your XML, and your parsers accept it, it's not really a problem.

Stack Overflow's and Notepad++'s syntax highlighter treat it like a multi-line comment, C++'s Boost library treats it as a multi-line comment, and the only parser I've found so far that breaks is the one in .NET, specifically C#. So, be sure to first test that your tools, IDE, libraries, language, etc. accept it before using it.

If you care about SGML compatibility, simply use this instead:

<!-- -
<XmlTag variable="0" />
<!- -->

Add '->' to the top comment and a '-' to the bottom comment. The downside is having to edit the bottom comment each time, which would probably make it easier to just type in <!-- at the top and --> at the bottom each time.

I also want to mention that other commenters recommend using an XML editor that allows you to right-click and comment/uncomment blocks of XML, which is probably preferable over fancy find/replace tricks (it would also make for a good answer in itself, but I've never used such tools. I just want to make sure the information isn't lost over time). I've personally never had to deal with XML enough to justify having an editor fancier than Notepad++, so this is totally up to you.

8
  • 3
    Unfortunately, this is wrong. The spec says, For compatibility, the string " -- " (double-hyphen) MUST NOT occur within comments.
    – kojiro
    Jun 26, 2013 at 20:01
  • 2
    Of course it's not legal. The spec is clear on that. XML parsers may follow Postel's Law and let you get away with it, but a compliant well-formedness checker will flag it.
    – kojiro
    Jun 27, 2013 at 1:31
  • 4
    As @kojiro says, a comment containing "--" is not well-formed XML. The phrase about "For compatibility" is merely the spec editors stating their reason for including this horrible rule: it does not mean you can ignore the rule if you aren't interested in compatibility. Downvoting the answer as it is wrong. Jun 27, 2013 at 7:21
  • 11
    You could write it <!-- -> and have it be a matter of adding a dash instead of having non-XML.
    – kojiro
    Jun 27, 2013 at 13:07
  • 3
    @RyanWH carry on doing what works for you, but you seem to be depending on a bug in the XML parser you are using. Personally, I use an IDE (oXygen) that allows me to select a piece of XML text, right click, and ask to comment it out. Which seems a lot easier than your technique. Jun 28, 2013 at 16:54
15

It is the same as the HTML or JavaScript block comments:

<!-- The to-be-commented XML block goes here. -->
7

Not orthodox, but it works for me sometimes; set your comment as another attribute:

<node usefulAttr="foo" comment="Your comment here..."/>
0
5

The Extensible Markup Language (XML) 1.0 only includes the block comments.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.