129

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Roam
  • 4,511
  • 9
  • 36
  • 68
  • 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. – Michael Kay Jun 27 '13 at 07: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 03 '14 at 08:51

5 Answers5

153

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.

Community
  • 1
  • 1
kojiro
  • 67,745
  • 16
  • 115
  • 177
  • 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](http://en.wikipedia.org/wiki/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.) – Peter Mortensen Dec 16 '19 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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Shaz
  • 1,347
  • 8
  • 18
  • Good tip! would work just abt as useful if i sandwich every XmL line between a pair of comment tags. maybe a line comment wd be worked in a new version of XML. – Roam Jun 26 '13 at 18:30
  • 3
    Unfortunately, this is wrong. [The spec](http://www.w3.org/TR/REC-xml/#sec-comments) says, *For compatibility, the string " -- " (double-hyphen) MUST NOT occur within comments.* – kojiro Jun 26 '13 at 20:01
  • @kojiro If you don't care about compatibility with SGML, then it is perfectly legal. I have yet to see any errors pop up by using this technique with various xml parsers. – Shaz Jun 26 '13 at 21:02
  • 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 '13 at 01: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. – Michael Kay Jun 27 '13 at 07:21
  • 2
    @MichaelKay I'll leave the answer as is, because I still think it's valid. Usually when I need to add/remove a line or a block of XML, it's merely for debugging purposes. I'm not advocating putting this in production level code, but it remains a powerful debugging tool. – Shaz Jun 27 '13 at 12:46
  • 10
    You could write it ` – kojiro Jun 27 '13 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. – Michael Kay Jun 28 '13 at 16:54
12

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

<!-- The to-be-commented XML block goes here. -->
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
nassim
  • 308
  • 2
  • 8
5

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

<node usefulAttr="foo" comment="Your comment here..."/>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
j rdl
  • 69
  • 1
  • 1
4

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

Fox32
  • 11,159
  • 8
  • 47
  • 69