尝试从XML文档中删除节点,但仅删除子节点 [英] Trying to remove a node from an XML document but it removes children only

查看:110
本文介绍了尝试从XML文档中删除节点,但仅删除子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一个XML文件



< Player>

< Name> John Rooks< / Name> ;

< TeleNo>

< / TeleNo>

< / Player>


< / Players>

我试图以粗体删除该位,使用此代码时curComboItem =John Rooks

 nsmgr =  XmlNamespaceManager(myPlayersDOM.NameTable)
Dim PlayerNameList As System.Xml.XmlNodeList = myPlayersDOM.GetElementsByTagName( Player
对于 每个节点作为 Xml.XmlNode PlayerNameList
xmlName = node.SelectSingleNode( 名称,nsmgr)
nodeName = xmlName.InnerText
如果 nodeName = curComboItem 那么
node.RemoveAll()
myPlayersDOM.Save( D:\ Myy Documents on D \ Visual Studio Projects \Match Management Application \Players.xml
退出 对于
结束 如果
下一步节点



我得到的是这个



< Player>

< / Player>


< / Players>

哪个最终导致null异常。



任何想法我做错了什么?

解决方案

Soluion是替换节点.RemoveAll()with node.ParentNode.RemoveChild (节点)

This a piece of my XML file

<Player>
<Name>John Rooks</Name>
<TeleNo>
</TeleNo>
</Player>

</Players>
I am trying to remove the bit in bold, using this code when curComboItem = " John Rooks"

nsmgr = New XmlNamespaceManager(myPlayersDOM.NameTable)
Dim PlayerNameList As System.Xml.XmlNodeList =          myPlayersDOM.GetElementsByTagName("Player")
For Each node As Xml.XmlNode In PlayerNameList
    xmlName = node.SelectSingleNode("Name", nsmgr)
    nodeName = xmlName.InnerText
    If nodeName = curComboItem Then
        node.RemoveAll()
        myPlayersDOM.Save("D:\My Documents on D\Visual Studio Projects\Match Management Application\Players.xml")
        Exit For
    End If
Next node


What I get is this

<Player>
</Player>

</Players>
Which ultimately results in a null exception.

Any ideas what I am doing wrong??

解决方案

Soluion is to replace node.RemoveAll() with node.ParentNode.RemoveChild(node)


这篇关于尝试从XML文档中删除节点,但仅删除子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆