如何从 XmlDocument 中删除所有注释标签
我如何从 XmlDocument 实例中删除所有注释标签?
有没有比检索 XmlNodeList 并迭代它们更好的方法?
XmlNodeList list = xmlDoc.SelectNodes("//comment()");
foreach(XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}
How would i go about to remove all comment tags from a XmlDocument instance?
Is there a better way than retrieving a XmlNodeList and iterate over those?
XmlNodeList list = xmlDoc.SelectNodes("//comment()");
foreach(XmlNode node in list)
{
node.ParentNode.RemoveChild(node);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
加载 xml 时,可以使用 XmlReaderSettings
上现有实例,您的解决方案看起来不错。
When you load the xml, you can use XmlReaderSettings
On an existing instance, your solution looks good.
不,就是这样,尽管我倾向于先将节点放入列表中。
我不确定
XmlNodeList
的 .NET 实现,但我知道以前的 MSXML 实现以惰性方式加载列表,并且过去的代码(例如上面的代码)最终会以某种方式失败枚举 List 时修改 DOM 树的视图。Nope thats about it, although I'd be inclind to place the nodes in a List first.
I'm not sure about the .NET implementation of
XmlNodeList
but I know that previous MSXML implementations loaded the list in lazy manner and code such as the above in the past would end up failing in some way as result of the DOM tree being modified as the List is enumerated.今天在寻找如何从 Visual Basic for Applications(不是 C#)中提取
的方法,我还发现了 nodeTypeString 属性,但它占用更多空间。下面是 VBA 中的一个示例:
它省略了文档顶部父注释节点,但如果需要,可以以某种方式直接检索它们,例如使用
With xmldoc.documentElement.childNodes
。Today looking for the way how to extract
<!-- -->
from Visual Basic for Applications (not C#), I have found also nodeTypeString property, but it takes more space. Here is an example in VBA:It omitts document top parent comment nodes, but they can be retrieved somehow directly if needed, for example using
With xmldoc.documentElement.childNodes
.