[2005] Delete XML Nodes in a Loop
Hi,
Given the following XML snippet
Code:
<Parent>
<Child LIID="1" />
<Child LIID="2" />
<Child LIID="3" />
<Child LIID="4" />
<Child LIID="5" />
</Parent>
how do I loop through all of the child controls and delete them one by one? I tried using
Code:
For Each ndeChild As XmlNode in ndeParent.ChildNodes
ndeParent.RemoveChild(ndeChild)
Next
but doing a "RemoveChild" while looping causes the For Each loop to exit on the very first child node. Any ideas?
Thanks!
Re: [2005] Delete XML Nodes in a Loop
Hey,
This is one way of doing it:
Code:
Dim xmlDoc As New XmlDocument()
Private Sub deleteXML_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xmlDoc.Load("XMLFile2.xml")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each xmlNode As XmlNode In xmlDoc.ChildNodes
xmlNode.RemoveAll()
Next
MessageBox.Show(xmlDoc.InnerXml.ToString())
End Sub
This results in just the parent node being displayed. The RemoveAll Method of the XmlNode object removes everything.
Hope this helps!!!
Gary
Re: [2005] Delete XML Nodes in a Loop
Hey,
I think you might be missing the point I was trying to make. RemoveAll works only on the instance of the XmlNode that it is currently looking at. It doesn't remove all the nodes.
I have modified the code slightly as follows:
Code:
Dim xmlDoc As New XmlDocument()
Private Sub deleteXML_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xmlDoc.Load("XMLFile2.xml")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each node As XmlNode In xmlDoc.SelectNodes("/Parent/Child")
If node.Attributes("LIID").Value = 1 Then
node.RemoveAll()
End If
Next
MessageBox.Show(xmlDoc.InnerXml.ToString())
End Sub
This is now conditional on the LIID and the above results in the first child node only being removed.
Give it a try and let me know if this is what you want.
Gary
Re: [2005] Delete XML Nodes in a Loop
My bad, I haven't woke up properly this morning :)
Give this a try:
Code:
Dim xmlDoc As New XmlDocument()
Private Sub deleteXML_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xmlDoc.Load("XMLFile2.xml")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each node As XmlNode In xmlDoc.SelectNodes("/Parent/Child")
If node.Attributes("LIID").Value = 1 Then
node.ParentNode.RemoveChild(node)
End If
Next
MessageBox.Show(xmlDoc.InnerXml.ToString())
End Sub
Hope that helps!!!
Gary
Re: [2005] Delete XML Nodes in a Loop
The code that I posted doesn't break out of the loop, did you try it? The result in the MessageBox is:
Code:
<Parent>
<Child LIID="2" />
<Child LIID="3" />
<Child LIID="4" />
<Child LIID="5" />
</Parent>
I don't see why it would do anything else. I also tried the code above without the If Statement and the result was:
Gary
Re: [2005] Delete XML Nodes in a Loop
No probs!! Glad you got it sorted!!