C# XDocument如何对元素进行注释

C# XDocument如何对元素进行注释,c#,linq-to-xml,C#,Linq To Xml,我只是想知道如何使用XDocument注释整个元素 XDocument doc = XDocument.Parse("<configuration> <connectionString> ... </connectionString> <configuration>"); /*Something like that*/ doc.Root.Element("connectionStrings").Node

我只是想知道如何使用XDocument注释整个元素

XDocument doc = XDocument.Parse("<configuration>
      <connectionString>
          ...
      </connectionString>
<configuration>");

/*Something like that*/ doc.Root.Element("connectionStrings").NodeType = XComment; /*??*/
XDocument doc=XDocument.Parse(“
...
");
/*类似于*/doc.Root.Element(“ConnectionString”).NodeType=XComment;/**/

类似这样的事情,也许:

var element = doc.Root.Element("connectionStrings");
element.ReplaceWith(new XComment(element.ToString()));
输入/输出示例:

之前:

<root>
  <foo>Should not be in a comment</foo>
  <connectionStrings>
    <nestedElement>Text</nestedElement>
  </connectionStrings>
  <bar>Also not in a comment</bar>
</root>

不应该在评论中
文本
也不在评论中
之后:

<root>
  <foo>Should not be in a comment</foo>
  <!--<connectionStrings>
  <nestedElement>Text</nestedElement>
</connectionStrings>-->
  <bar>Also not in a comment</bar>
</root>

不应该在评论中
也不在评论中
如果您想


这就是你要找的吗?

大概是这样的:

var element = doc.Root.Element("connectionStrings");
element.ReplaceWith(new XComment(element.ToString()));
输入/输出示例:

之前:

<root>
  <foo>Should not be in a comment</foo>
  <connectionStrings>
    <nestedElement>Text</nestedElement>
  </connectionStrings>
  <bar>Also not in a comment</bar>
</root>

不应该在评论中
文本
也不在评论中
之后:

<root>
  <foo>Should not be in a comment</foo>
  <!--<connectionStrings>
  <nestedElement>Text</nestedElement>
</connectionStrings>-->
  <bar>Also not in a comment</bar>
</root>

不应该在评论中
也不在评论中
如果您想


这就是你要找的吗?

诅咒你,斯基特!我也在写同样的东西,但后来你来了,写得更快更好。哈哈,+1!我希望所有子节点都在注释替换中?@Cedric:它们已经在注释部分中了。(如果你在上面列出所有的子元素,它不会显示
nestedElement
)诅咒你,Skeet!我也在写同样的东西,但后来你来了,写得更快更好。哈哈,+1!我希望所有子节点都在注释替换中?@Cedric:它们已经在注释部分中了。(如果上面列出了所有子元素,则不会显示
nestedElement