Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 283803 - Last Review: October 12, 2001 - Revision: 1.0
PRB: Testing the Return Value of the selectSingleNode Method in Visual Basic
This article was previously published under Q283803
On This Page
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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=305019/EN-US/
)
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 "<?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
- Save the project and run it.
- 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.
- 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
- 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.
- 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.
- 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.
APPLIES TO
- Microsoft XML Parser 2.0
- Microsoft XML Parser 2.5
- Microsoft XML Parser 2.6
- Microsoft XML Parser 3.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
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please