PRB: Mixing Different Versions of MSXML DOM Objects Is Not Recommended

Symptoms

The Microsoft XML (MSXML) Document Object Model (DOM) objects exposed by the MSXML parser can be used to programmatically create, load, and manipulate XML documents. Avoid mixing DOM objects from different versions of the MSXML parser because it is not a recommended coding practice. If you attempt to mix different versions of the MSXML DOM objects while programming the DOM, one of the following error messages appears:
Run-time error '-2147024809(80070057)': The parameter is incorrect
Run-time error '-2147467262(80004002)': No such interface supported

Cause

When you mix different versions of MSXML DOM objects in a DOM object's method call, the object from the differing version of the parser that is supplied as a required method parameter is treated as a foreign object.

Resolution

Reference and use objects implemented by a single version of the MSXML parser. Do not mix different versions of DOM Objects when you program the MSXML DOM.

More Information

Steps to Reproduce Behavior

Execute the following steps to set up a Microsoft Visual Basic sample that reproduces the error messages that appear in the "Symptoms" section of this article. The code in the sample uses versions 2.6 and 3.0 of the MSXML parser. You need to have both versions of the MSXML parser installed on your computer to reuse the code without modifying the ProgIDs of the DOM objects. You may also reproduce the behavior mixing MSXML version 2.6 with 4.0, 3.0 with 4.0, and so on.

  1. Create a new Standard EXE project in Visual Basic 6.0. Form1 is created by default.
  2. On the Project menu, set a reference to Microsoft XML v2.6.
  3. Add a CommandButton on Form1 and label the button Error 1.
  4. Copy and paste the following code into the Click event procedure of Error 1:
    Dim doc26 As MSXML2.DOMDocument26
    Dim node26 As MSXML2.IXMLDOMNode

    Set doc26 = New MSXML2.DOMDocument26
    doc26.loadXML "<Books><Book>XML Programming</Book></Books>"

    Set node26 = doc26.childNodes(0)

    Dim doc30 As Object
    Set doc30 = CreateObject("MSXML2.DOMDocument.3.0")
    node26.appendChild doc30.createNode(NODE_ELEMENT, "Book", "")

    MsgBox doc26.xml
  5. The preceding code loads well-formed XML into an instance of an MSXML 2.6 DOMDocument object. It then attempts to append an MSXML 3.0 IXMLDOMNode object to a child node of the MSXML 2.6 DOMDocument.
  6. Save and run the project. Click Error 1 to generate the following error message on the node26.appendChild statement:
    Run-time error '-2147024809(80070057)': The parameter is incorrect
  7. Stop the execution of the project.
  8. Add a second CommandButton to Form1 and label it Error 2.
  9. Open an empty text file in Microsoft Notepad.
  10. Copy and paste the following XML code into Notepad and save the file as books.xml in the folder of your choice:
    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="books.xsl"?>
    <books>
    <book>
    <title>XML Step by Step</title>
    </book>
    <book>
    <title>Mastering XML</title>
    </book>
    </books>
  11. Open another empty file in Notepad.
  12. Copy and paste the following XSL code into Notepad and save the file as books.xsl in the same folder where you saved books.xml:
    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="books">
    <html>
    <body>
    <h1>A list of books</h1>
    <table>
    <xsl:apply-templates/>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="book">
    <tr>
    <td><xsl:value-of select="./title"/></td>
    </tr>
    </xsl:template>

    </xsl:stylesheet>
  13. Copy and paste the following code into the Click event procedure of the Error 2:
    Dim xmldoc As MSXML2.DOMDocument26
    Dim xsldoc As Object

    Set xmldoc = New MSXML2.DOMDocument26
    xmldoc.Load "<path to books.xml>"

    Set xsldoc = CreateObject("MSXML2.DOMDocument.3.0")
    xsldoc.Load "<path to books.xsl>"

    Debug.Print xmldoc.transformNode(xsldoc)
    In the preceding code, you need to insert the paths to books.xml and books.xsl in the statements that load the XML and XSL documents into two instances of the DOMDocument object.

    This code loads the XML document into an instance of the MSXML 2.6 DOMDocument object. It then loads the XSL document into an instance of the MSXML 3.0 DOMDocument object and attempts to execute the XSL transform by mixing the DOMDocument objects from the two different versions of the MSXML parser.

  14. Save and run the project. Click Error 2 to generate the following error message from the transformNode method of the MSXML 2.6 DOMDocument object:
    Run-time error '-2147467262(80004002)': No such interface supported
Eigenschaften

Artikelnummer: 284856 – Letzte Überarbeitung: 22.06.2014 – Revision: 1

Feedback