|
-
Oct 16th, 2008, 10:19 PM
#1
Thread Starter
Fanatic Member
[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!
-
Oct 16th, 2008, 11:30 PM
#2
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
-
Oct 17th, 2008, 08:20 AM
#3
Thread Starter
Fanatic Member
gep13 aka Gary ....
Thanks for the reply and the "RemoveAll" I'm sure would work, but I can't use it because the removal needs to be conditional. I made my example simple for the purpose of explanation, but I do need to remove the items one at a time based on the LIID.
Any other ideas anyone?
-
Oct 17th, 2008, 09:33 AM
#4
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
-
Oct 17th, 2008, 10:09 AM
#5
Thread Starter
Fanatic Member
gep13 aka Gary ....
I tried your technique and the result was that the "LIID" attribute was removed but not the node itself. In other words, the result would look like this:
Code:
<Parent>
<Child />
<Child LIID="2" />
<Child LIID="3" />
<Child LIID="4" />
<Child LIID="5" />
</Parent>
This is consistent with the definition of "RemoveAll": "Removes all of the child nodes and/or attributes of the current node." I want the <Child /> node to go too, not just strip the "LIIID" attribute from it.
Thanks.
-
Oct 17th, 2008, 10:20 AM
#6
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
-
Oct 17th, 2008, 10:43 AM
#7
Thread Starter
Fanatic Member
Back to Square One ...
gep13, "node.ParentNode.RemoveChild(node)" is essentially the same as "ndeParent.RemoveChild(ndeChild)", which is what I originally posted. Therefore, the issue is the same: "doing a "RemoveChild" while looping causes the For Each loop to exit on the very first child node.".
It looks like I'm going to have to think of a different way to do this.
-
Oct 17th, 2008, 10:45 AM
#8
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
-
Oct 17th, 2008, 11:31 AM
#9
Thread Starter
Fanatic Member
gep13 aka Gary ....
Thanks for helping me figure this out. The issue was the way that I was creating the list of child nodes to be looped through. I was doing it this way:
Code:
Dim ndeParent As System.Xml.XmlNode = xmlDoc.SelectSingleNode("//Parent")
If Not ndeParent Is Nothing AndAlso ndeParent.ChildNodes.Count > 0 Then
For Each ndeChild As System.Xml.XmlNode In ndeParent.ChildNodes
ndeChild.ParentNode.RemoveChild(ndeChild)
Next
End If
When I used your method for selecting nodes
Code:
For Each node As XmlNode In xmlDoc.SelectNodes("/Parent/Child")
it worked:
Thanks again for your help.
-
Oct 17th, 2008, 11:36 AM
#10
Re: [2005] Delete XML Nodes in a Loop
No probs!! Glad you got it sorted!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|