开发者

Need help stopping MSXML from adding namespaces

开发者 https://www.devze.com 2022-12-16 11:08 出处:网络
I am using MSXML 4 to generate the following xml string: <?xml version=\"1.0\"> <Parent_Element xmlns=\"http://1\">

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?


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号