MS Dynamics NAV XML Namespace is set automatically
-
25-05-2021 - |
Question
I'm creating a new XML File out of a table. The problem is I don't want the children of the root node to have the attribute xmlns. This happens automatically and its incorrect. If i have the attribute on the root element, no matter how it gets there it puts it in its child-nodes. How can I get rid of it?
A
Locals Name DataType Subtype Length
L_DataXML Automation 'Microsoft XML, v6.0'.DOMDocument60
L_XMLElement Automation 'Microsoft XML, v6.0'.IXMLDOMElement
L_XMLAttribute Automation 'Microsoft XML, v6.0'.IXMLDOMAttribute
L_XMLNode Automation 'Microsoft XML, v6.0'.IXMLDOMNode
L_XMLOrderHeader Automation 'Microsoft XML, v6.0'.IXMLDOMNode
L_XMLOrderItemList Automation 'Microsoft XML, v6.0'.IXMLDOMNode
L_XMLOrderSummary Automation 'Microsoft XML, v6.0'.IXMLDOMNode
C/AL CODE
L_DataXML.loadXML('<ORDER xmlns="test"/>');
L_XMLNode := L_DataXML.documentElement;
L_XMLOrderHeader := L_DataXML.createNode(1,'ORDER_HEADER','');
L_XMLNode.appendChild(L_XMLOrderHeader);
XML
<?xml version="1.0"?>
<ORDER xmlns="test">
<ORDER_HEADER xmlns=""/>
</ORDER>
Solution
I had this problem too a while ago. Unforunatley, there is no way you can prevent that using XMLDOM. My workaround was to create the XML File with the error, load it into a bigtext variable and loop through it while erasing xmlns=""
Code:
XMLFile.TEXTMODE(FALSE);
XMLFile.OPEN(SavePath);
XMLFile.CREATEINSTREAM(InStream);
InputText.READ(InStream);
XMLFile.CLOSE;
TextToFind := 'xmlns=""';
TextPos := InputText.TEXTPOS(TextToFind);
WHILE TextPos <> 0 DO BEGIN
InputText.GETSUBTEXT(SubText, 1, TextPos - 1);
OutputText.ADDTEXT(SubText);
InputText.GETSUBTEXT(InputText, TextPos + STRLEN(TextToFind));
TextPos := InputText.TEXTPOS(TextToFind);
END;
OutputText.ADDTEXT(InputText);
XMLFile.TEXTMODE(FALSE);
XMLFile.CREATE(SavePath);
XMLFile.CREATEOUTSTREAM(OutStream);
OutputText.WRITE(OutStream);
XMLFile.CLOSE;
Variables
Name DataType Subtype Length
XMLFile File
InStream InStream
OutStream OutStream
InputText BigText
OutputText BigText
SubText BigText
TextPos Integer
TextToFind Text 30
SavePath Text 1024
Hope it helps
OTHER TIPS
Thanks for the answer. Thats true there is no way of getting ridd of it! But i found a workaround to allow at least the valiadion of the file. Adding the default namespace from the root allows the document to be valid.
<?xml version="1.0"?>
<ORDER xmlns="test">
<ORDER_HEADER xmlns="test"/>
</ORDER>
another issue which someone else might come across was a second namespace
<?xml version="1.0"?>
<ORDER xmlns="test" xmlns:second="second-test">
<ORDER_HEADER xmlns="test">
<second:TAG/>
</ORDER_HEADER>
</ORDER>
at first it results to an error because it doesnt find the second namespace. Just add the namespace URI to the AddFunciton and it wont complain anymore.
Cheers
when use createNode method use namespaceURI from parent node. probably should work if you would change to L_XMLOrderHeader := L_DataXML.createNode(1,'ORDER_HEADER',L_DataXML.namespaceURI);