Microsoft KB Archive/283803

= PRB: Testing the Return Value of the selectSingleNode Method in Visual Basic =

Article ID: 283803

Article Last Modified on 10/12/2001

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft XML Parser 2.5
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Parser 3.0 Service Pack 1
 * Microsoft XML Core Services 4.0
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



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.  Open a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click References, and then add a reference to Microsoft XML, v3.0. Add a Command button to Form1.  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 &quot;<Book>XML Programming</Book></Books>&quot;

Set mnode = doc.selectSingleNode(&quot;//magazine&quot;) 'Set mnode = doc.selectSingleNode(&quot;//Book&quot;)

If IsNull(mnode) Then Debug.Print &quot;No nodes selected&quot; Else Debug.Print mnode.Text End If

Set doc = Nothing </li> Save the project and run it.</li> 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 (&quot;//magazine&quot;) 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.

</li>  Stop the execution of the project, and replace the conditional If statement with the following code: If mnode Is Nothing Then Debug.Print &quot;No nodes selected&quot; Else Debug.Print mnode.Text End If                   </li> 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.</li> 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.</li> 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.</li></ol>

Keywords: kbprb KB283803

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.