Microsoft KB Archive/290485

= PRB: DOMDocument.CreateNode Method Appends an Empty Namespace Declaration =

Article ID: 290485

Article Last Modified on 10/13/2001

-

APPLIES TO


 * 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

-



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=&quot;&quot;] 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 &quot;Symptoms&quot; section and test the suggested resolution:  Open a new Standard EXE project in Visual Basic. Form1 is created by default. Set a project reference to Microsoft XML, v3.0. Drag-and-drop a Command button onto Form1, and make the caption Display XML.  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, &quot;Books&quot;, &quot;urn-MyServer-Books&quot;)

Set doc.documentElement = node

Set node = doc.createNode(NODE_ELEMENT, &quot;Book&quot;, &quot;&quot;) doc.documentElement.appendChild node

MsgBox doc.xml  Save and execute the Visual Basic project.  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=&quot;urn-MyServer-Books&quot;><Book xmlns=&quot;&quot;/></Books> </li>  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, &quot;Book&quot;, &quot;urn-MyServer-Books&quot;) </li>  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=&quot;urn-MyServer-Books&quot;><Book/></Books> </li></ol>

Keywords: kbprb KB290485

-

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

© Microsoft Corporation. All rights reserved.