PRB: DOMDocument.CreateNode() Method Appends an Empty Namespace Declaration (290485)



The information in this article applies to:

  • Microsoft XML 2.5
  • Microsoft XML 2.6
  • Microsoft XML 3.0
  • Microsoft XML 3.0 SP1
  • Microsoft XML 4.0

This article was previously published under Q290485

SYMPTOMS

Creating an eXtensible Markup Language (XML) element by using the MSXML DOMDocument.CreateNode() method and appending it as a child node of another element that references a specified default namespace generates an empty namespace declaration [xmlns=""] for the newly created and appended child element.

CAUSE

This behavior is by design. It occurs only when the parent node references a specified default namespace, and a blank string is supplied as the namespaceURI parameter of the DOMDocument.CreateNode() method that is used to create the child element. The blank string supplied as the namespaceURI parameter is treated as the explicit default namespace for the child element.

RESOLUTION

Specify the parent element's namespaceURI as the namespaceURI parameter of the DOMDocument.CreateNode() method to indicate that the parent's namespaceURI applies to the child, and to prevent the generation of the empty namespace declaration for the child element.

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 a sample Visual Basic project that can be used to reproduce the behavior described in the "Symptoms" section and test the suggested resolution:
  1. Open a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Set a project reference to Microsoft XML, v3.0.
  3. Drag-and-drop a Command button onto Form1, and make the caption Display XML.
  4. Copy-and-paste the following code into the Click event procedure of the Command button:
    Dim doc As MSXML2.DOMDocument
    Dim node As MSXML2.IXMLDOMNode
    
    Set doc = New MSXML2.DOMDocument
    Set node = doc.createNode(NODE_ELEMENT, "Books", "urn-MyServer-Books")
    
    Set doc.documentElement = node
    
    Set node = doc.createNode(NODE_ELEMENT, "Book", "")
    doc.documentElement.appendChild node
    
    MsgBox doc.xml
    					
  5. Save and execute the Visual Basic project.
  6. Click Display XML on the form to execute the code that uses the MSXML Document Object Model (DOM) to generate the following sample XML, which is then displayed in a Message box. Note that the namespace declaration of the child <Book> element is generated as an empty string:
    <Books xmlns="urn-MyServer-Books"><Book xmlns=""/></Books>
  7. Stop the project, and then modify the call to the CreateNode() method that is used to create the child <Book> element in the Command button's Click event procedure as follows. Note that the namespaceURI of the parent <Books> element is supplied as the namespaceURI parameter to indicate that the child <Book> element will inherit the parent element's default namespace:
    Set node = doc.createNode(NODE_ELEMENT, "Book", "urn-MyServer-Books")
    					
  8. Save and execute the project, and then click Display XML to display the following XML. Note that the namespace declaration of the child <Book> element is not generated as an empty string:
    <Books xmlns="urn-MyServer-Books"><Book/></Books>
    					

Modification Type:MajorLast Reviewed:10/13/2001
Keywords:kbDSupport kbprb KB290485 kbAudDeveloper