removing children via Microsoft.XMLDOM

Posted on 2006-03-20
Last Modified: 2012-08-14
I'm creating an XML doc with the Microsoft.XMLDOM object.  As I work my way thru the building doc, I create <level1Child> nodes (per the example below).  Then I create <sample> nodes, and finally <more> nodes.  If the data for the <more> nodes does not meet a certain criteria, I don't need to add the <more> node.  Due to the nature of my code, I've already added the <level1Child> node and the <sample> node.

The problem is, sometimes after working my way thru, I may have some <level1Child> nodes that do not have any <more> nodes underneath them - see the 2nd <level1Child> as an example.  I need a quick-n-dirty way to look at the document once I'm done building it, and examine it to see which <level1Child> nodes do not have any <more> nodes underneath them.  For each one that doesn't, I need to remove that node and any <sample> nodes.  

          <more>data 1</more>
          <more>data 2</more>
          <more>data 3</more>
          <more>data 4</more>
          <more>data 5</more>
          <more>data 6</more>
          <more>data 7</more>
          <more>data 8</more>
Question by:BigMonkeyHead
    LVL 52

    Accepted Solution


       '// Assuming an XMLDOM object named oDom

        Set oNodes = oDom.selectNodes("//level1Child[count(sample/more)=0]")
        For Each node In oNodes
           node.parentNode.RemoveChild node

    This might not be 100% cos i'm doing it from memory :o)
    LVL 1

    Author Comment

    Thanks - that's pretty much what I was looking for.  :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now