Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to remove an xml tag

Status
Not open for further replies.

Programming2007

Programmer
Nov 10, 2006
24
US
I am trying to remove a whole element but instead am just removing its attributes. For example my code will take the tag: <item key="dbName" value="TESTdb" /> and after removeall it leaves it as: <item />.
I want the item tag totally erased. How do I do this? Here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<sections>
<comment> ***** All settings are CaSe SeNsItIvE ***** </comment>
<section name="ApplicationCABS">
<item />
<item key="dbSchema" value="xxxx" />
<item />
<item key="dbBadTable" value="xxxx" />
<item key="dbGoodTable" value="xxxx" />
<item key="dbName" value="xxxxx" />
<item key="badSID" value="xxxxx" />
</section>
<section name="ApplicationDUF">
<item key="dbSchema" value="yyyy" />
<item />
<item key="dbName" value="yyyy" />
<item key="dbBadTable" value="yyyy" />
<item key="dbGoodTable" value="yyyy" />
</section>
<section name="Application">
<item />
<item key="badSID" value="zzz" />
<item key="goodSID" value="zzz" />
<item key="dbName" value="zzz" />
<item key="dbPassword" value="zzz" />
<item key="dbSchema" value="zzz" />
<item key="dbBadTable" value="zzz" />
<item key="dbGoodTable" value="zzz" />
</section>
</sections>


Here is the code:
Dim NodebadSID_cab As Xml.XmlNode = document.DocumentElement.SelectSingleNode("//sections/section[1]/item[@key='badSID']")
document.DocumentElement.ParentNode.RemoveChild(NodebadSID_cab)


I also tried this:
Dim navigator As Xml.XPath.XPathNavigator = document.CreateNavigator()
navigator.MoveToRoot()
'Move to the first child node (comment field).
navigator.MoveToFirstChild()

'nav = navigator.SelectSingleNode("//section")
If navigator.NodeType = Xml.XPath.XPathNodeType.Element Then
'If children exist.
If navigator.HasChildren Then

'Move to the first child.
navigator.MoveToFirstChild()

Do
Dim nodeBlankItem As Xml.XmlNode
If (document.DocumentElement.SelectSingleNode("//sections/section[1]/item").Value = "") Then
Try
nodeBlankItem = document.DocumentElement.SelectSingleNode("//sections/section[1]//item")
document.DocumentElement.SelectSingleNode("//sections/section[1]//item").RemoveChild(nodeBlankItem)
Catch ex As Exception

End Try
End If

Loop While navigator.MoveToNext()
End If
End If

I tried this too:

'Dim objNodeList As Xml.XmlNodeList = document.DocumentElement.SelectNodes("//sections/section[1]/item")
'Dim node As XmlNode
'For Each node In objNodeList
'document.DocumentElement.RemoveChild(node)
next


Does anyone know how I can delete the entire item tag when the item tag does not have any attributes with this xml file???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top