Reputation: 500
In the code below, if I remove the xmlns part and only allow "version" to be created as an attribute on the root element of Worklist, everything is ok. However as soon as I add the xmlns attribute, then every second level element appears to inherit an attribute of xmlns= "www.someURL.com/XMLSchema"
If I only have version, all is ok. If I have xmlns on it's own or if I have both (as in the code below) I get the problem. I've included two xml snippets below the code, one with the code below, and the other with the xmlns code removed.
I can fudge this by not including it in the XML creation and just add it as a string after the entire xml document is created, but I'd like to understand how to prevent this (because the program that uses the resultant XML breaks if xmlns is not an attribute on Worklist, but it also breaks if xmlns IS an attribute on any other tag).
I've also read some other Stack Overflow posts (and other resources) related to the use of XML Namespaces and how they affect attributes, which I understand but in this instance I need to be able to add this single attribute to the Worklist element ONLY.
Dim xmlDoc As MSXML2.DOMDocument60
Set xmlDoc = New MSXML2.DOMDocument60
Dim Attribute1 As IXMLDOMAttribute, Element1 As IXMLDOMElement
Set RootNode = xmlDoc.createElement("Worklist")
Set Attribute1 = xmlDoc.createAttribute("xmlns")
Attribute1.value = "www.someURL.com/XMLSchema"
RootNode.setAttributeNode Attribute1
Set Attribute1 = Nothing
Set Attribute1 = xmlDoc.createAttribute("version")
Attribute1.value = "1.0"
RootNode.setAttributeNode Attribute1
Set Attribute1 = Nothing
XML Snippets
<Worklist xmlns="www.someURL.com/XMLSchema" version="1.0">
<Options xmlns="" allow="false" delete="false" rename="false"/>
<Templates xmlns="">
</Templates>
<Sequence xmlns="" name="aName"/>
<Worklist version="1.0">
<Options allow="false" delete="false" rename="false"/>
<Templates>
</Templates>
<Sequence name="aName"/>
Thanks for reading !
Upvotes: 3
Views: 1040
Reputation: 19502
Use the createElementNS()
/createAttributeNS()
methods, they create the nodes for an specific namespace. The necessary namespace definitions will be added automatically.
Note: XML attributes can only have a namespace if defined with a prefix.
Upvotes: 1