xmlmsxmlmsxml4

Need help stopping MSXML from adding namespaces


I am using MSXML 4 to generate the following xml string:

<?xml version="1.0">
<Parent_Element xmlns="http://1">
    <Child_One>
        <Child_Two xmlns="http://2">
            <Child_Three>
            </Child_Three>
        </Child_Two>
    </Child_One>
</Parent>

However the output from my IXMLDOMDocument2Ptr always includes a namespace for Child_Three:

<?xml version="1.0">
<Parent_Element xmlns="http://1">
    <Child_One>
        <Child_Two xmlns="http://2">
            <Child_Three xmlns="http://1">
            </Child_Three>
        </Child_Two>
    </Child_One>
</Parent>

My understanding is that this behavior is part of the XML standard, but the system receiving the xml rejects it if the extra namespace is present. It will also reject the xml if there is an empty namespace (i.e. xmlns="").

Is there anyway in MSXML to avoid adding or removing the namespace for Child_Three?


Solution

  • I figured it out.

    1) I had a defect where the document namespace was used instead of the namespace in the parent node.

    2) With the fix from #1, I ended up with an empty namespace (xmlns=""). To corect this I had to set the namepace when the node is created. Before I was creating the node and then added the xmlns attribute in a separate call.

    Before:

    pNode->createNode(NODE_ELEMENT, name, "");
    pAttrib = pNode->createAttribute("xmlns")
    pAttrib->put_NodeValue(namespace)
    

    Now:

    pNode->createNode(NODE_ELEMENT, name, "namespace");