如何从 XmlDocument 中删除所有注释标签

发布于 2024-08-14 06:25:42 字数 250 浏览 2 评论 0原文

我如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛滥成性 2024-08-21 06:25:42

加载 xml 时,可以使用 XmlReaderSettings

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create("...", settings);
xmlDoc.Load(reader);

上现有实例,您的解决方案看起来不错。

When you load the xml, you can use XmlReaderSettings

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;
XmlReader reader = XmlReader.Create("...", settings);
xmlDoc.Load(reader);

On an existing instance, your solution looks good.

当梦初醒 2024-08-21 06:25:42

不,就是这样,尽管我倾向于先将节点放入列表中。

我不确定 XmlNodeList 的 .NET 实现,但我知道以前的 MSXML 实现以惰性方式加载列表,并且过去的代码(例如上面的代码)最终会以某种方式失败枚举 List 时修改 DOM 树的视图。

 foreach (var node in xml.SelectNodes("//comment()").ToList())
   node.ParentNode.RemoveChild(node);

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.

 foreach (var node in xml.SelectNodes("//comment()").ToList())
   node.ParentNode.RemoveChild(node);
南薇 2024-08-21 06:25:42

今天在寻找如何从 Visual Basic for Applications(不是 C#)中提取 的方法,我还发现了 nodeTypeString 属性,但它占用更多空间。下面是 VBA 中的一个示例:

Dim xmldoc As New MSXML2.DOMDocument30
Dim oNodeList As IXMLDOMSelection
Dim node As IXMLDOMNode
Dim i As Long

Dim FileName As String, FileName1 As String

FileName = "..." ' Source
FileName2 = "..." ' Target

xmldoc.async = False ' ?
xmldoc.Load FileName
If (xmldoc.parseError.errorCode <> 0) Then Exit Sub ' or Function

Set oNodeList = xmldoc.selectNodes("//*") '' all nodes

For i = 0 To oNodeList.length - 1
With oNodeList(i)

     For Each node In .childNodes
         If node.nodeTypeString = "comment" Then .removeChild node
     Next

End With
Next

xmldoc.Save FileName2

Set oNodeList = Nothing ' ?
Set xmldoc = Nothing

它省略了文档顶部父注释节点,但如果需要,可以以某种方式直接检索它们,例如使用 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:

Dim xmldoc As New MSXML2.DOMDocument30
Dim oNodeList As IXMLDOMSelection
Dim node As IXMLDOMNode
Dim i As Long

Dim FileName As String, FileName1 As String

FileName = "..." ' Source
FileName2 = "..." ' Target

xmldoc.async = False ' ?
xmldoc.Load FileName
If (xmldoc.parseError.errorCode <> 0) Then Exit Sub ' or Function

Set oNodeList = xmldoc.selectNodes("//*") '' all nodes

For i = 0 To oNodeList.length - 1
With oNodeList(i)

     For Each node In .childNodes
         If node.nodeTypeString = "comment" Then .removeChild node
     Next

End With
Next

xmldoc.Save FileName2

Set oNodeList = Nothing ' ?
Set xmldoc = Nothing

It omitts document top parent comment nodes, but they can be retrieved somehow directly if needed, for example using With xmldoc.documentElement.childNodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文