如何从XmlDocument中删除所有注释标记如何从XmlDocument实例中删除所有注释标记?有没有比检索XmlNodeList并迭代这些更好的方法?XmlNodeListlist=xmlDoc.SelectNodes("//comment()");foreach(列表中的XmlNode节点){node.ParentNode.RemoveChild(node);加载xml时,可以使用XmlReaderSettingsXmlReaderSettingssettings=newXmlReaderSettings();设置.IgnoreComments=true;XmlReaderreader=XmlReader.Create("...",settings);xmlDoc.Load(阅读器);在现有实例上,您的解决方案看起来不错。无论如何,尽管我倾向于先将节点放入列表中。我不确定XmlNodeList的.NET实现,但我知道以前的MSXML实现以惰性方式加载列表,并且过去的代码会如上所述失败,因为以某种方式修改了DOM树。列表是枚举的。foreach(varnodeinxml.SelectNodes("//comment()").ToList())node.ParentNode.RemoveChild(node);今天查看如何从VisualBasicforApplications(不是C#)中提取,我找到了nodeTypeString属性,但它需要更多空间。下面是VBA中的示例:DimxmldocAsNewMSXML2.DOMDocument30DimoNodeListAsIXMLDOMSelectionDimnodeAsIXMLDOMNodeDimiAsLongDimFileNameAsString,FileName1AsStringFileName="..."'SourceFileName2="..."'目标xmldoc.async=False'?xmldoc.LoadFileNameIf(xmldoc.parseError.errorCode0)ThenExitSub'orFunctionSetoNodeList=xmldoc.selectNodes("//*")''所有节点Fori=0TooNodeList.length-1WithoNodeList(i)ForEachnodeIn.childNodesIfnode.nodeTypeString="comment"Then.removeChildnodeNextEndWithNextxmldoc.SaveFileName2SetoNodeList=Nothing'?Setxmldoc=Nothing它省略了文档顶级父评论节点,但如果需要,可以以某种方式直接检索它们,例如使用Withxmldoc.documentElement.childNodes。以上就是C#学习教程:如何删除XmlDocument中的所有注释和标签。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。涉及侵权,请点击维权联系管理员删除。如需转载请注明出处:
