2

I want to create an xml document in VB6 similar to this:

<?xml version="1.0"?>
<AKU_BA_GETMEDBAL_REQUEST xmlns="http://xmlns.aku.edu/ps/sas/schemas/AKU_BA_GETMEDBAL_REQUEST.V1">
  <AKU_BA_MRNO>255-30-98</AKU_BA_MRNO>
    <AKU_BA_SYS_LOCATION>"karachi"</AKU_BA_SYS_LOCATION>
    <AKU_BA_CASHLESS_LOCK>"N"</AKU_BA_CASHLESS_LOCK>
    <AKU_BA_GET_BALANCE>"N"</AKU_BA_GET_BALANCE>
</AKU_BA_GETMEDBAL_REQUEST>

And I am using this code:

Dim objDOM As New MSXML2.DOMDocument30
Dim objNode As MSXML2.IXMLDOMNode
Dim objPerson As MSXML2.IXMLDOMNode
Dim objGrandChildNode As MSXML2.IXMLDOMNode
Dim objAttribute As MSXML2.IXMLDOMAttribute
Dim objElement As MSXML2.IXMLDOMElement
' Create the main xml node
Set objNode = objDOM.createNode(NODE_PROCESSING_INSTRUCTION, "xml", "")
objDOM.appendChild objNode
Set objNode = objDOM.createNode(NODE_ELEMENT, Request1, Request2)
Dim i As Integer
For i = 1 To AttributeCollection.Count
    Set objPerson = objDOM.createNode(NODE_ELEMENT, AttributeCollection.Item(i), "")
    objPerson.Text = AttributeCollection.Item(i + 1)
    objNode.appendChild objPerson
    i = i + 1
Next i
objDOM.appendChild objNode
MsgBox objDOM.xml

Where collections have the data - but it is creating a result like this:

<?xml version="1.0"?>
<AKU_BA_GETMEDBAL_REQUEST xmlns="http://xmlns.aku.edu/ps/sas/schemas/AKU_BA_GETMEDBAL_REQUEST.V1">
  <AKU_BA_MRNO xmlns="">255-30-98</AKU_BA_MRNO>
    <AKU_BA_SYS_LOCATION xmlns="">"karachi"</AKU_BA_SYS_LOCATION>
    <AKU_BA_CASHLESS_LOCK xmlns="">"N"</AKU_BA_CASHLESS_LOCK>
    <AKU_BA_GET_BALANCE xmlns="">"N"</AKU_BA_GET_BALANCE>
</AKU_BA_GETMEDBAL_REQUEST>

What can I do because it creates extra xmlns="" on every Person node which I don't want. Is there any other way to do that?

1 Answer 1

1

This behavior is by design. It occurs only when the parent node references a specified default namespace, and a blank string is supplied as the namespaceURI parameter of the DOMDocument.CreateNode() method that is used to create the child element. The blank string supplied as the namespaceURI parameter is treated as the explicit default namespace for the child element.

Specify the parent element's namespaceURI as the namespaceURI parameter of the DOMDocument.CreateNode() method to indicate that the parent's namespaceURI applies to the child, and to prevent the generation of the empty namespace declaration for the child element.

Set objPerson = objDOM.createNode(NODE_ELEMENT, AttributeCollection.Item(i), "urn-FooBar")

For further information and to reproduce it see MS Knowledge Base

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.