How can I comment a single line in XML?

XmlXml Comments

Xml Problem Overview


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 https://stackoverflow.com/questions/2757396/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.

Xml Solutions


Solution 1 - Xml

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.

Solution 2 - Xml

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.

Solution 3 - Xml

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

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

Solution 4 - Xml

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

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

Solution 5 - Xml

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoamView Question on Stackoverflow
Solution 1 - XmlkojiroView Answer on Stackoverflow
Solution 2 - XmlShazView Answer on Stackoverflow
Solution 3 - XmlnassimView Answer on Stackoverflow
Solution 4 - Xmlj rdlView Answer on Stackoverflow
Solution 5 - XmlFox32View Answer on Stackoverflow