72

Are these nested comments allowed in a XML file?

<!-- Making only one observation attempting to correct the error code -->
<!-- <component>
       <!-- Result observation template -->
            <!-- <id root="2.16.840.1.113883.19.5.10" extension="103220"/>
     </component> -->
bluish
  • 24,718
  • 26
  • 114
  • 174
Laxmikanth Samudrala
  • 2,143
  • 4
  • 25
  • 44
  • 1
    ... wondering why noone mentioned using [`CDATA`](http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean) as a hacky block comment ... – Martin Ba Apr 18 '16 at 08:36

5 Answers5

59

No, the string -- is not permitted to appear within comments in XML. So the fact you have -- show up inside another comment is going to cause failures.

And trying to post that answer also broke the text entry parsing ;)

For further proof, check the W3C specification:

http://www.w3.org/TR/2008/REC-xml-20081126/#sec-comments

The phrase

For compatibility, the string " -- " (double-hyphen) MUST NOT occur within comments.]

appears in the first paragraph of the section on XML comments.

bluish
  • 24,718
  • 26
  • 114
  • 174
Brent Writes Code
  • 18,046
  • 6
  • 48
  • 56
  • 2
  • 1
    System.Xml.XmlException: This is an invalid comment syntax. – b w Aug 28 '09 at 17:14
  • 2
    It's to ensure compatibility with SGML. – Brent Writes Code May 14 '10 at 22:06
  • 11
    @Brent Nash So how do you comment out a big block that has comments in it? – David Doria Sep 10 '13 at 19:39
  • 1
    @DavidDoria Unfortunately, I don't think you can do it easily. It's like trying to comment out a chunk of code with `/* */` that has another `/* */` nested inside it somewhere. I think your best bet is to find an XML editor that can auto insert/remove comments for you on a per line basis and fix the few outliers yourself. It is unpleasant at best. – Brent Writes Code Sep 11 '13 at 02:16
  • 5
    @BrentNash I'll take your word for it that it's just to ensure backward compatibility :). Still, for a format designed in the 90s, not allowing nested comments is not cool. – Manav Feb 18 '15 at 06:06
  • @BrentWritesCode: Agree with Manav. The spec *should* have allowed for single-line comments like C and BASIC already had for *DECADES* prior. Then, IDEs (like VS already does with C# even though C# has a multi-line comment feature), keep adding more single-line comment prefixes to each line even if multiple lines are marked for commenting. That's a no brainer. The possibility of the resulting HTML, XML, XAML, etc. being in error is no excuse. The same problem exists with any programming language that allows single-line (or even multi-line) comments. It's the programmer's responsibility. – Tom Jul 24 '19 at 18:06
  • @BrentWritesCode: Yes, but XML is all about nesting things! XML is designed so we can describe nested stuff!!! Didn't they think nesting is a good thing??? (twelve years late comment) :-) – André Caldas Nov 27 '21 at 16:44
40

As it is said in How do I comment out a block of tags in XML?, you can try to wrap your code with a non-existing processing-instruction, e.g.:

<?ignore
<component>
       <!-- Result observation template -->
            <!-- <id root="2.16.840.1.113883.19.5.10" extension="103220"/>
     </component> 
?>
Community
  • 1
  • 1
psychoslave
  • 2,397
  • 2
  • 25
  • 41
  • This is a great help for cases when badly-behaved NuGet packages mutilate my lovingly curated `web.config` files by adding their own elements and dozens of comments! – Dai Jul 20 '17 at 07:19
5

In a word - no.

The first encountered end-comment marker will, er... end the comment and the rest of it will look somewhat unpleasant from there on out.

annakata
  • 72,622
  • 16
  • 112
  • 180
4

You can't. -- both starts and terminates a comment. This makes nesting them impossible.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
1

Notepad++ together with the plugin XML Tools can do this.

Select a block of xml and on the xml tools submenu selected "Comment Selection".

Each existing "inner xml comment" will be altered so that it looks like this

  <!{1}** inner xml comment **{1}>

and if you add another outer comment in this way, those original inner comments will be further altered to

  <!{2}** inner xml comment **{2}>
Philip Beck
  • 335
  • 2
  • 10