Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 828928 - Last Review: August 18, 2005 - Revision: 1.1
PRB: MSXML 4.0 Sets the XML Namespace Attribute to an Empty Value for Child Nodes
On This Page
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 = "") 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 "Symptoms" 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:
<?xml version='1.0'?>
<Collection>
<Book>
<Science>
<Title>Lover Birds</Title>
<Author>Cynthia Randall</Author>
<Publisher>Lucerne Publishing</Publisher>
</Science>
</Book>
</Collection>
- Start Visual Basic 6.0.
- Create a Standard EXE project. By default,
Form1 is created.
- On the Project menu, click
References.
- In the References - Project1 dialog box,
click to select
Microsoft XML, v4.0 under Available References, and then click
OK.
- Add a command button to Form1, and then set the
Caption property of the command button to
XMLApp.
- 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 ("c:\book.xml")
xmldom3.documentElement.setAttribute "xmlns", "x-schema:books"
MsgBox ("msxml 3 : " + xmldom3.xml)
xmldom4.Load ("c:\book.xml")
xmldom4.documentElement.setAttribute "xmlns", "x-schema:books"
MsgBox ("msxml 4 : " + xmldom4.xml)
- On the Run menu, click
Start. Form1 is displayed.
- On the Form1 form, click
XMLApp.
You receive a message box that contains the
following content:
msxml 3 : <?xml
version="1.0"?>
<Collection
xmlns="x-schema:books">
<Book>
<Science>
<Title>Lover
Birds</Title>
<Author>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 = "") is
not applied to the child node of the root node. - Click OK to close this message
box.
You receive another message box that contains the following
content:
msxml 4: <?xml
version="1.0"?>
<Collection xmlns="x-schema:books">
<Book
xmlns="">
<Science>
<Title>Lover
Birds</Title>
<Author>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 = "") is
applied to the child node of the root node. - Click OK to close this message
box.
REFERENCES
For additional information, click the following article
number to view the article in the Microsoft Knowledge Base:
290485Â
(http://kbalertz.com/Feedback.aspx?kbNumber=290485/
)
DOMDocument.CreateNode() Method Appends an Empty Namespace Declaration
For more information about XML namespaces, visit
the following W3C Web site:
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
| kbnamespace kbxml kbprb KB828928 |
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
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please