Microsoft KB Archive/828928

= PRB: MSXML 4.0 Sets the XML Namespace Attribute to an Empty Value for Child Nodes =

Article ID: 828928

Article Last Modified on 8/18/2005

-

APPLIES TO


 * Microsoft XML Core Services 4.0
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-





SYMPTOMS
When you add the XML namespace attribute (xmlns) to the root node of an XML document, an XML namespace attribute with an empty value is created for the child nodes of the root node.



CAUSE
You cannot add, modify, or delete an XML namespace definition in an instance of an XML document after the document has been loaded into the XML Document Object Model (XMLDOM) parser. The XML nodes that are used to represent data in the XML document are created when the document is loaded into the XMLDOM parser. These nodes are permanently bound to their XML namespace attributes when they are created. Therefore, the empty XML namespace declaration (xmlns = &quot;&quot;) is appended to the child nodes of these nodes to preserve the default XML namespace attribute of these nodes.



WORKAROUND
To work around this problem, specify XML namespace declarations before you load an XML document.



STATUS
This behavior is by design.



MORE INFORMATION
The behavior that is mentioned in the &quot;Symptoms&quot; section of this article is the expected behavior in the MSXML 4.0 Document Object Model (DOM). This behavior also complies with the World Wide Web Consortium (W3C) specifications. The MSXML 4.0 DOM specifies the following clauses:
 * XML namespace definitions are specified at the load time of an XML document.
 * Microsoft does not recommend that you programmatically create or append XML namespace attributes because this may result in inconsistent behavior.
 * The XML nodes that are used to represent data in an XML document are created when the document is loaded into the XMLDOM parser. These XML nodes are permanently bound to their XML namespace attributes when they are created.
 * Adding a default XML namespace attribute to a root node after data is loaded into the XMLDOM parser does not automatically associate all child nodes with the default XML namespace.
 * To dynamically add an element or an attribute node to the XML tree, pass the XML namespace attribute as the third parameter to the CreateNode method.

Steps to Reproduce the Behavior
  Paste the following XML code in Notepad, and then save the file as book.xml :     Lover Birds Cynthia Randall <Publisher>Lucerne Publishing</Publisher> </Science> </Book> </Collection> </li> <li>Start Visual Basic 6.0.</li> <li>Create a Standard EXE project. By default, Form1 is created.</li> <li>On the Project menu, click References.</li> <li>In the References - Project1 dialog box, click to select Microsoft XML, v4.0 under Available References, and then click OK.</li> <li>Add a command button to Form1, and then set the Caption property of the command button to XMLApp .</li> <li> Add the following code to the Click event of the XMLApp command button: Dim xmldom3 As New DOMDocument30 Dim xmldom4 As New DOMDocument40

xmldom3.Load (&quot;c:\book.xml&quot;) xmldom3.documentElement.setAttribute &quot;xmlns&quot;, &quot;x-schema:books&quot; MsgBox (&quot;msxml 3 : &quot; + xmldom3.xml)

xmldom4.Load (&quot;c:\book.xml&quot;) xmldom4.documentElement.setAttribute &quot;xmlns&quot;, &quot;x-schema:books&quot; MsgBox (&quot;msxml 4 : &quot; + xmldom4.xml) </li> <li>On the Run menu, click Start. Form1 is displayed.</li> <li>On the Form1 form, click XMLApp.

You receive a message box that contains the following content:

msxml 3 : <?xml version=&quot;1.0&quot;?>

<Collection xmlns=&quot;x-schema:books&quot;>

<Book>

<Science>

Lover Birds</Title>

Cynthia Randall</Author>

<Publisher>Lucerne Publishing</Publisher>

</Science>

</Book>

</Collection>

The content that is displayed is the output from the MSXML 3.0 parser. Notice that the XML namespace attribute(xmlns = &quot;&quot;) is not applied to the child node of the root node.</li> <li>Click OK to close this message box.

You receive another message box that contains the following content:

msxml 4: <?xml version=&quot;1.0&quot;?>

<Collection xmlns=&quot;x-schema:books&quot;>

<Book xmlns=&quot;&quot;>

<Science>

Lover Birds</Title>

Cynthia Randall</Author>

<Publisher>Lucerne Publishing</Publisher>

</Science>

</Book>

</Collection>

The content that is displayed is the output from the MSXML 4.0 parser. Notice that the XML namespace attribute (xmlns = &quot;&quot;) is applied to the child node of the root node.</li> <li>Click OK to close this message box.</li></ol>

<div class="references_section">