首页 > 解决方案 > 逐行注释“块”

问题描述

(背景)许多语言有两种注释语法。例如,在 C# 或 C++ 中,您可以执行以下操作:

/*void Method(int arg)
{
  Validate(arg);
  Log.Add("Validated");
  DoWork(arg);
}*/

或者:

//void Method(int arg)
//{
//  Validate(arg);
//  Log.Add("Validated");
//  DoWork(arg);
//}

许多人更喜欢后者,因为更清楚的是每一行都被注释掉了。(背景结束)


现在,在 XML 中,您只有一种(相当糟糕的)注释方式。但是 in 仍然可以以不同的方式使用。

假设我有这个 XML 文档:

<?xml version="1.0"?>
<Zootheque>
  <Animal Name="Elephant">
    <Descrption>Is really huge and has a long and flexible nose.</Descrption>
    <NumberOfLegs>4</NumberOfLegs>
  </Animal>
  <Animal Name="Snake">
    <Descrption>Will make you (Eve) eat forbidden fruit.</Descrption>
    <NumberOfLegs>0</NumberOfLegs>
  </Animal>
  <Animal Name="Ostrich">
    <Descrption>You cannot see it if its head is buried in sand.</Descrption>
    <NumberOfLegs>2</NumberOfLegs>
  </Animal>
</Zootheque>

假设我想注释掉关于Snake. 在 Microsoft Visual Studio 和一些简单的文本编辑器中,当我选择相关的“块”行并选择注释掉选定的行或类似内容时,我得到:

<?xml version="1.0"?>
<Zootheque>
  <Animal Name="Elephant">
    <Descrption>Is really huge and has a long and flexible nose.</Descrption>
    <NumberOfLegs>4</NumberOfLegs>
  </Animal>
  <!--<Animal Name="Snake">
    <Descrption>Will make you (Eve) eat forbidden fruit.</Descrption>
    <NumberOfLegs>0</NumberOfLegs>
  </Animal>-->
  <Animal Name="Ostrich">
    <Descrption>You cannot see it if its head is buried in sand.</Descrption>
    <NumberOfLegs>2</NumberOfLegs>
  </Animal>
</Zootheque>

我觉得上面的评论风格有时是有问题的。相反,我想要:

<?xml version="1.0"?>
<Zootheque>
  <Animal Name="Elephant">
    <Descrption>Is really huge and has a long and flexible nose.</Descrption>
    <NumberOfLegs>4</NumberOfLegs>
  </Animal>
  <!--<Animal Name="Snake">-->
  <!--  <Descrption>Will make you (Eve) eat forbidden fruit.</Descrption>-->
  <!--  <NumberOfLegs>0</NumberOfLegs>-->
  <!--</Animal>-->
  <Animal Name="Ostrich">
    <Descrption>You cannot see it if its head is buried in sand.</Descrption>
    <NumberOfLegs>2</NumberOfLegs>
  </Animal>
</Zootheque>

问题 1:后一种 XML 注释样式有名称吗?(如果我需要用谷歌搜索这会有所帮助。)

问题 2: Visual Studio 是否支持通过单次击键或鼠标单击(在选择行之后)创建这种注释?或者您可以推荐的其他一些 (XML) 文本编辑器支持?


后来的补充:我刚刚发现(一个足够新的版本)记事本++有这个功能。您选择多行并使用单行注释(Ctrl+K) 或切换单行注释(Ctrl+Q)。

标签: xmlcomments

解决方案


我认为(1)和(2)的答案是“不”和“不”,但很难证明不存在。

确实,无法嵌套注释是设计上的弱点(XML 试图与 SGML 兼容,而兼容性意味着故意重复其他人的错误,正如 David Wheeler 曾经说过的那样。)我使用过的各种 XML 编辑器都有各种尝试来解决问题,但没有一个是完美的。


推荐阅读