Nested comments in XML?

34,605

Solution 1

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.

Solution 2

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> 
?>

Solution 3

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.

Solution 4

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

Solution 5

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}>
Share:
34,605
Laxmikanth Samudrala
Author by

Laxmikanth Samudrala

simple

Updated on July 05, 2022

Comments

  • Laxmikanth Samudrala
    Laxmikanth Samudrala almost 2 years

    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> -->
    
  • annakata
    annakata over 14 years
    <!-- this is not true -- or is it? -->
  • b w
    b w over 14 years
    <!-- it -- is an error --> System.Xml.XmlException: This is an invalid comment syntax.
  • Brent Writes Code
    Brent Writes Code almost 14 years
    It's to ensure compatibility with SGML.
  • David Doria
    David Doria over 10 years
    @Brent Nash So how do you comment out a big block that has comments in it?
  • Brent Writes Code
    Brent Writes Code over 10 years
    @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.
  • Manav
    Manav about 9 years
    @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.
  • Dai
    Dai almost 7 years
    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!
  • Tom
    Tom almost 5 years
    @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.
  • André Caldas
    André Caldas over 2 years
    @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) :-)