删除XML文件中的节点?
发布时间:2020-12-16 22:52:03 所属栏目:百科 来源:网络整理
导读:我有xml文件,想要删除一些节点: group First group /First group Second group Name /Name Name /Name Name /Name /Second group/group 我想删除节点名称,因为以后我想创建新节点. 以下是我拥有的代码: Dim doc As New XmlDocument()Dim nodes As XmlNodeL
我有xml文件,想要删除一些节点:
<group> <First group> </First group> <Second group> <Name> </Name> <Name> </Name> <Name> </Name> </Second group> </group> 我想删除节点名称,因为以后我想创建新节点. 以下是我拥有的代码: Dim doc As New XmlDocument() Dim nodes As XmlNodeList doc.Load("doc.xml") nodes = doc.SelectNodes("/group") Dim node As XmlNode For Each node In nodes node = doc.SelectSingleNode("/group/Second group/Name") If node IsNot Nothing Then node.ParentNode.RemoveNode(node) doc.Save("doc.xml") End If Next 解决方法
部分问题是XML无效.
Naming Elements and Attributes 元素名称不能包含空格. 假设有效的XML元素名称,即:First_group,Second_group,以下代码将从Second_group中删除所有子项 Dim doc As New XmlDocument() Dim nodes As XmlNodeList doc.Load("c:tempnode.xml") nodes = doc.SelectNodes("/group/Second_group") For Each node As XmlNode In nodes If node IsNot Nothing Then node.RemoveAll() doc.Save("c:tempnode.xml") End If Next 或LINQ to XML: Dim doc As XDocument = XDocument.Load("c:tempnode.xml") doc.Root.Element("Second_group").Elements("Name").Remove() doc.Save("c:tempnode.xml") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |