我只想知道如何使用XDocument对整个元素进行注释。
XDocument doc = XDocument.Parse("<configuration>
<connectionString>
...
</connectionString>
<configuration>");
/*Something like that*/ doc.Root.Element("connectionStrings").NodeType = XComment; /*??*/
答案 0 :(得分:5)
这样的事情,也许是:
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>
如果您想要添加换行符......
那是你在找什么?