PRB: Testing the Return Value of the selectSingleNode Method in Visual Basic (283803)



The information in this article applies to:

  • Microsoft XML 2.0
  • Microsoft XML 2.5
  • Microsoft XML 2.6
  • Microsoft XML 3.0
  • Microsoft XML 3.0 SP1
  • Microsoft XML 4.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q283803

SYMPTOMS

The MSXML SDK documentation states that the selectSingleNode method of the DOMDocument, IXMLDOMNode, and IXMLDOMElement objects return a NULL if no node that matches the specified query string is found in the loaded XML document. Testing the return value of the selectSingleNode method for a NULL value by using the IsNull() function in Visual Basic returns False even when the method does not return a node that matches the specified query string. Subsequent code that attempts to access the properties and methods of the returned node object based on the return value of the IsNull() conditional test generates the following error message when the selectSingleNode method does not return a matching node:
Run-time error '91' : Object variable or With block variable not set.

CAUSE

A NULL value is an atomic constant that indicates the absence of a value. A NULL value cannot be assigned to object variables by using a Set objvar = Null Visual Basic statement. The selectSingleNode method returns a reference to an IXMLDOMNode object representing the first node that matches the specified query string. In the event of no nodes that match the specified query string, the target IXMLDOMNode object variable to which the result of executing the method is assigned remains uninitialized. It is not assigned a NULL value.

RESOLUTION

Use the Is Nothing conditional expression to test the object variable to which the return value of selectSingleNode is assigned to determine whether a matching node was identified in the loaded XML document.

STATUS

This behavior is by design.

MORE INFORMATION

If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:

305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds

Steps to Reproduce Behavior

Execute the following steps to set up and test a Visual Basic project that demonstrates the specified problem and the suggested resolution. The following code sample uses version 3.0 of the MSXML parser. The Prog IDs of the MSXML DOM objects have to be modified as required if you are using an older version of the MSXML parser.
  1. Open a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. On the Project menu, click References, and then add a reference to Microsoft XML, v3.0.
  3. Add a Command button to Form1.
  4. Copy and paste the following code into the Click event procedure of the Command button:
    Dim doc As MSXML2.DOMDocument30
    Dim mnode As MSXML2.IXMLDOMNode
    
    Set doc = New MSXML2.DOMDocument30
    doc.loadXML "<?xml version='1.0'?><Books><Book>XML Programming</Book></Books>"
    
    Set mnode = doc.selectSingleNode("//magazine")
    'Set mnode = doc.selectSingleNode("//Book")
    
    If IsNull(mnode) Then
     Debug.Print "No nodes selected"
    Else
     Debug.Print mnode.Text
    End If
    
    Set doc = Nothing
    					
  5. Save the project and run it.
  6. Click the Command button to execute the code that loads the sample XML string and calls the selectSingleNode method of the DOMDocument object. The query string ("//magazine") that is specified in the call to selectSingleNode does not match any of the nodes in the loaded XML.NOTE: The IsNull(mnode) conditional test returns False even though no matching node was returned by the selectSingleNode method, and the code generates the specified error when attempting to write out the Text property of the node object to the Visual Basic Debug window.

  7. Stop the execution of the project, and replace the conditional If statement with the following code:
    If mnode Is Nothing Then
     Debug.Print "No nodes selected"
    Else
     Debug.Print mnode.Text
    End If
    					
  8. Save and run the project, and note that the conditional test returns the desired result. The message No nodes selected is displayed in the Visual Basic Debug window as expected.
  9. Comment out the first call to the selectSingleNode method that searches for 'magazine' elements, and uncomment the second call that searches for 'Book' elements that do exist in the loaded XML.
  10. Save and run the project, and note that the text of the first Book element (XML Programming) in the loaded XML is written out to the Visual Basic Debug window as required.

Modification Type:MajorLast Reviewed:10/12/2001
Keywords:kbDSupport kbprb KB283803