SelectSingleNode fails with MS XML 4.

Posted on 2004-11-20
Last Modified: 2009-07-29

Currently, I am experiencing some problems with selecting nodes.
I am invoking the XML parser using:

Set objXMLParser = CreateObject("Msxml2.DOMDocument.4.0")

objXMLParser.ASync = False
objXMLParser.PreserveWhiteSpace = False
objXMLParser.ResolveExternals = False
objXMLParser.ValidateOnParse = True

Then, later in the code, I use:

Set objXMLChild = Nothing
Set objXMLChild = objXMLItems.ChildNodes.Item(lngCounter)
If Not objXMLChild Is Nothing Then
  Select Case objXMLChild.NodeName
    Case "TotalResults"
      lngTotalNumberOfResults = CLng(objXMLChild.Text)
    Case "Item"

      Set objXMLSubChild = Nothing
      Set objXMLSubChild = objXMLChild.SelectSingleNode("LargeImage")

      If Not objXMLSubChild Is Nothing Then
        Set objXMLLargeImageURL = Nothing
        Set objXMLLargeImageURL = objXMLSubChild.SelectSingleNode("URL")
        If Not (objXMLLargeImageURL Is Nothing) Then
          astrLargeImageURL(1) = objXMLLargeImageURL.Text
        End If
      End If
  End Select
End If


If Not objXMLSubChild Is Nothing Then

always returns false.

If I change the initialization part from

Set objXMLParser = CreateObject("Msxml2.DOMDocument.4.0")


Set objXMLParser = CreateObject("MSXML.DOMDocument")

everything works as expected. Any idea what could be wrong?

Question by:Sebastian_Mares

    Author Comment

    If you need a demo, .

    When you launch the program as-is, it will tell you that it wasn't able to find any images. If you change the XML initialization to MSXML.DOMDocument, it will download Cover.jpg to the application's folder. The SelectSingleNode part returns True when running XML 2 (or whatever MSXML.DOMDocument is) and False when XML 4 or 3 is used.
    LVL 52

    Expert Comment

    by:Carl Tawn
    I think its something to do with the namespace.

    If you change then line:

        Set objXMLSubChild = objXMLChild.SelectSingleNode("LargeImage")


        Set objXMLSubChild = objXMLChild.SelectSingleNode("*[local-name()='LargeImage']")

    Then it should work OK.
    LVL 52

    Accepted Solution

    It appears to be due to the default namespace on the document.
    If you setup an alias for the default namespace, then you should be able to use that for selecting nodes.

    Add the line:
            objXMLParser.setProperty "SelectionNamespaces", "xmlns:x=''"

    After the line:

            objXMLParser.validateOnParse = True

    Then change:

            Set objXMLSubChild = objXMLChild.SelectSingleNode("LargeImage")


           Set objXMLSubChild = objXMLChild.SelectSingleNode("x:LargeImage")

    This should then allow you to select the nodes properly using the 'x:' prefix.

    Hope this helps.

    Author Comment


    Author Comment

    Do you know if the URL changed?
    LVL 52

    Expert Comment

    by:Carl Tawn  <--  This isn't necessarily a valid URL, its simply a namespace.  A namespace is a way of uniquely naming elements from a specific supplier in order to avoid naming conflicts.  A namespace can be anything you want, just so long as it is unique.  A lot of developers will choose a namespace that incorporates their company URL so that it will be unique.

    Hope this clears things up a bit. Namespaces are a bit odd until you get used to them.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 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

    16 Experts available now in Live!

    Get 1:1 Help Now