XmlDocument createElement method with only namespace prefix, without nsValue in the tag

133 views Asked by At

I have this xml header where tag is the root:

<?xml version="1.0" encoding="UTF-8"?>
<CBIBdyPaymentRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01 CBIBdyPaymentRequest.00.04.01.xsd" xmlns:PMRQ="urn:CBI:xsd:CBIPaymentRequest.00.04.01" xmlns="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
  <CBIEnvelPaymentRequest>
    <CBIPaymentRequest>
      <PMRQ:GrpHdr xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">

I need the tag to have just PMRQ prefix without the attribute "xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01", but I can't find a method that lets me add just the prefix to the tag. The createElement method asks for nameSpacePrefix, id and nsValue.

How can I obtain just PMRQ:GrpHdr ?

I'm working on X++ (Microsoft Dynamics Ax), but methods should be the same in .NET

I obtained this

<?xml version="1.0" encoding="UTF-8"?>
<CBIBdyPaymentRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01 CBIBdyPaymentRequest.00.04.01.xsd" xmlns:PMRQ="urn:CBI:xsd:CBIPaymentRequest.00.04.01" xmlns="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
  <CBIEnvelPaymentRequest>
    <CBIPaymentRequest>
      <PMRQ:GrpHdr xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
        <PMRQ:MsgId>G182581</PMRQ:MsgId>  
        [...]
      </PMRQ:GrpHdr>
      <PMRQ:PmtInf xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
        <PMRQ:PmtMtd>TRF</PMRQ:PmtMtd>
      </PMRQ:PmtInf>
    </CBIPaymentRequest>
  </CBIEnvelPaymentRequest>
</CBIBdyPaymentRequest>

And I can't understand why, using same methods, I dont have the xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01" part in children tags under GrpHdr and PmtInf

EDIT: I've tried a more complete test. The code I used is the following

xmlDoc = new XmlDocument();
    xmlDoc.appendChild(xmlDoc.createProcessingInstruction("xml", 'version="1.0" encoding="UTF-8"'));

    nodeRoot = xmlDoc.appendChild(xmlDoc.createElement('CBIBdyPaymentRequest')); //'CBIBdyPaymentRequest'

    //1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attribute
    nodeRoot.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");

    //2. xsi:schemaLocation="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01 CBIBdyPaymentRequest.00.04.01.xsd"
    schemaLocationAttribute = xmlDoc.createAttribute2("xsi:schemaLocation", "http://www.w3.org/2001/XMLSchema-instance");
    schemaLocationAttribute.value('urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01 CBIBdyPaymentRequest.00.04.01.xsd');
    nodeRoot.setAttributeNode(schemaLocationAttribute);

    //3. xmlns:PMRQ="urn:CBI:xsd:CBIPaymentRequest.00.04.01" attribute
    nodeRoot.setAttribute("xmlns:PMRQ","urn:CBI:xsd:CBIPaymentRequest.00.04.01");

    //4. xmlns="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01" attribute
    nodeRoot.setAttribute("xmlns","urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01");

    nodeRoot2 = nodeRoot.appendChild(xmlDoc.createElement('CBIEnvelPaymentRequest')); //'CBIEnvelPaymentRequest'
    nodeRoot3 = nodeRoot2.appendChild(xmlDoc.createElement('CBIPaymentRequest')); //'CBIPaymentRequest'
    nodeRoot3.prefix("PMRQ");

    nodeGrpHdr = nodeRoot3.appendChild(xmlDoc.createElement3("PMRQ", "GrpHdr","urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01"));

    //msgId
    xeTmp = nodeGrpHdr.appendChild(xmlDoc.createElement3("PMRQ","MsgId","urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01"));
    xeTmp.appendChild(xmlDoc.createTextNode("test string"));

    xeTmp = nodeGrpHdr.appendChild(xmlDoc.createElement3("PMRQ","Tmp","urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01"));
    xeTmp.appendChild(xmlDoc.createTextNode("test string"));

    //
    nodeGrpHdr2 = nodeRoot3.appendChild(xmlDoc.createElement3("PMRQ", "GrpHdr2","urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01"));
    xeTmp = nodeGrpHdr2.appendChild(xmlDoc.createElement3("PMRQ","MsgId2","urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01"));
    xeTmp.appendChild(xmlDoc.createTextNode("test string"));

The xml it generates is :

<?xml version="1.0" encoding="UTF-8"?>
<CBIBdyPaymentRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01 CBIBdyPaymentRequest.00.04.01.xsd" xmlns:PMRQ="urn:CBI:xsd:CBIPaymentRequest.00.04.01" xmlns="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
    <CBIEnvelPaymentRequest>
        <CBIPaymentRequest>
            <PMRQ:GrpHdr xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
                <PMRQ:MsgId>test string</PMRQ:MsgId>
                <PMRQ:Tmp>test string</PMRQ:Tmp>
            </PMRQ:GrpHdr>
            <PMRQ:GrpHdr2 xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01">
                <PMRQ:MsgId2>test string</PMRQ:MsgId2>
            </PMRQ:GrpHdr2>
        </CBIPaymentRequest>
    </CBIEnvelPaymentRequest>
</CBIBdyPaymentRequest>

I should not have the xmlns:PMRQ="urn:CBI:xsd:CBIBdyPaymentRequest.00.04.01" part in GrpHdr and GrpHdr2 tags. Why they have that attribute since they are created the same way used for MsgId, Tmp and MsgId2 tags ?

0

There are 0 answers