如何在 XML 中注释一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17327120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:05:16  来源:igfitidea点击:

How can I comment a single line in XML?

xmlxml-comments

提问by Roam

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.

XML 中是否有行注释?所以,没有更接近的,比如编译器使用的“//”。

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

我看到了如何在 XML 中注释掉一个标签块?和其他一些讨论。

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

这种类型的注释可以很实用地注释掉一行而不打扰它靠近某个地方。

回答by kojiro

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

不,没有办法在 XML 中注释一行并让注释在换行符处自动结束。

XML has only one definition for a comment:

XML 只有一个注释定义

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

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

XML 禁止--在注释中保持与 SGML 的兼容性

回答by Shaz

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

正如其他人所说,没有办法在 XML 中合法地进行单行注释来注释多行,但是,有一些方法可以更容易地注释掉 XML 段。

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 将被取消注释。删除“>”并再次注释掉。这是我见过的最简单的方法来快速注释/取消注释 XML 而不会破坏事物。

<!-- --
<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 SGMLand 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.

额外的好处是您只操作顶部评论,底部评论可以永远坐在那里。这破坏了与SGML 的兼容性,并且一些 XML 解析器会拒绝它。只要这不是您的 XML 中的永久固定装置,并且您的解析器接受它,它就不是真正的问题。

Stack Overflow's and Notepad++'ssyntax highlighter treat it like a multi-line comment, C++'s Boostlibrary 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.

Stack Overflow 和Notepad++ 的语法高亮将其视为多行注释,C++ 的Boost库将其视为多行注释,目前我发现的唯一一个中断的解析器是 .NET 中的解析器,特别是 C#。所以,在使用之前一定要先测试你的工具、IDE、库、语言等是否接受它。

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

如果您关心 SGML 兼容性,只需使用它:

<!-- -
<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.

我还想提一下,其他评论者推荐使用 XML 编辑器,该编辑器允许您右键单击并注释/取消注释 XML 块,这可能比花哨的查找/替换技巧更可取(它本身也可以提供一个很好的答案,但我从未使用过此类工具。我只是想确保信息不会随着时间的推移而丢失)。我个人从来没有处理过足够多的 XML 来证明拥有一个比 Notepad++ 更酷的编辑器是合理的,所以这完全取决于你。

回答by nassim

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

它与 HTML 或 JavaScript 块注释相同:

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

回答by j rdl

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

不是正统的,但有时对我有用;将您的评论设置为另一个属性:

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

回答by Fox32

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

所述可扩展标记语言(XML)1.0仅包括块注释。