Microsoft Knowledge Base Email Alertz

Creating an eXtensible Markup Language (XML) element by using the MSXML

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 290485 - Last Review: October 13, 2001 - Revision: 1.1

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

This article was previously published under Q290485

On This Page

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  (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 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>
    					

APPLIES TO
  • 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
Keywords: 
kbprb KB290485
       

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