Need help stopping MSXML from adding namespaces

徘徊边缘 提交于 2019-12-11 12:13:45

问题


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?


回答1:


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");



回答2:


MSXML will represent exactly the namespaces you tell it to represent.

From your quotation, it looks as if you created the child3 node with a namespace of http://1, and you need to create it with a namespace of http://2.




回答3:


I found the solution to this problem. The issue is that MSXML cannot handle broken namespaces...

I had a situation recently where in the highest level tag, there was a xmlns="http://...", but this was wrong. It should have been: xmlns:xsd="http://...".

Once i fixed that in the topmost xml tag, i could insert xml tags into documents without seeing the xmlns="" everywhere.

Interestingly when you write a XML document from the start, create the hierarchy of tags, you wont get the xmlns="" tags.



来源:https://stackoverflow.com/questions/2079209/need-help-stopping-msxml-from-adding-namespaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!