Deleting all children of an XML node

Hi everyone,

I am using VBA for MS office. I would like to know how I can delete all children of a node. The method should not fail if the node has no children.

So, basically I have:

Dim xmlParser as Object
Set xmlParser = CreateObject("MSXML2.DOMDocument")
xmlParser.LoadXML (documentXMLData)
Dim serializedElment as Object
Set serializedElement = xmlParser.ChildNodes(0).selectSingleNode("SerializedData")

Now I want to delete all children (if any) of the serializedElement node.

How can I do this?


Who is Participating?
fds_fatboyConnect With a Mentor Commented:
Private Sub delChildren(element  As object)
    If element Is Nothing Then Exit Sub
    Do Until element.childNodes(0) Is Nothing
        element.removeChild element.childNodes(0)
End Sub
Private Sub delChildren(tvw As TreeView, nod As Node)
    If nod Is Nothing Then Exit Sub
    Do Until nod.Child Is Nothing
        tvw.Nodes.Remove nod.Child.Index
End Sub
Whoops wrong answer
And you would call it like this:

    delChildren serializedElement

Is there any reason you are doing all this using late-binding. Early binding would be more efficient and you would get the added help of being able to use intellisense.

Also I asked a question regarding a previous PAQ that you accepted from me and would appreciate an answer:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.