web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested answer

AL XML Generation - Namespace prefixes not being applied correctly

(3) ShareShare
ReportReport
Posted on by 79
I'm working with Business Central AL and trying to generate a SOAP XML request with specific namespace prefixes, but some elements aren't getting the prefixes applied correctly.
 
Problem
When generating XML, elements like Header, Body, and CustomerAccounts should have namespace prefixes (soapenv: and psp:), but they're either getting default namespaces or no prefixes at all.

 
Current Output
<Envelope
	xmlns:soapenv="http://hidden.com/"
	xmlns:abc="http://hidden.com"
	xmlns:123="http://hidden.com" xmlns="soapenv">
	<Header/ xmlns="soapenv">
	<Body xmlns="soapenv">
		<CustomerAccounts xmlns="abc">
			...
		</CustomerAccounts>
	</Body>
</Envelope>
Desired Output
<soapenv:Envelope
	xmlns:soapenv="http://hidden.com/"
	xmlns:abc="http://hidden.com"
	xmlns:123="http://hidden.com">
	<soapenv:Header/>
	<soapenv:Body>
		<abc:CustomerAccounts>
			...
		</abc:CustomerAccounts>
	</soapenv:Body>
</soapenv:Envelope>
AL Code
local procedure CreateBaseXML(
        namespacePrefix: Text;
        namespaceURI: Text
    ): XmlDocument
    var
        XMLDoc: XmlDocument;
        XMLDec: XmlDeclaration;
        XMLElem: XmlElement;
        XMLElem2: XmlElement;
        XMLAtr: XmlAttribute;
    begin
        XMLDoc := XmlDocument.Create();
        XMLDec := XmlDeclaration.Create('1.0', 'UTF-8', 'yes');
        XMLDoc.SetDeclaration(XMLDec);

        XMLElem := XmlElement.Create('Envelope', namespacePrefix);
        XMLAtr := XmlAttribute.CreateNamespaceDeclaration('123', 'http://hidden.com/');
        XMLElem.Add(XMLAtr);
        XMLAtr := XmlAttribute.CreateNamespaceDeclaration('abc', 'http://hidden.com/');
        XMLElem.Add(XMLAtr);
        XMLAtr := XmlAttribute.CreateNamespaceDeclaration(namespacePrefix, namespaceURI);
        XMLElem.Add(XMLAtr);

        XmlElem2 := XmlElement.Create('Header', namespacePrefix);
        XMLElem.Add(XmlElem2);

        XMLElem2 := XmlElement.Create('Body', namespacePrefix);
        XMLElem.Add(XMLElem2);

        XMLDoc.Add(XMLElem);
        exit(XMLDoc);
    end;
Assume namespacePrefix and namespaceURI is set accordingly i.e., CreateBaseXML('soapenv', 'https://soapenv.com');
 
What I've Tried
  1. Using XmlElement.Create('prefix:ElementName') syntax
  2. Adding namespace declarations with CreateNamespaceDeclaration
  3. Different combinations of namespace parameters in element creation
How can I ensure the namespace prefixes are applied correctly to all appropriate elements in AL-generated XML?
 
 
I have the same question (0)
  • Verified answer
    DK-27050928-0 Profile Picture
    79 on at
    AL XML Generation - Namespace prefixes not being applied correctly
    I created the Xml elements and passed in the Prefix rather than the Uri i.e.,

    It was:
    XmlElem2 := XmlElement.Create('Header', namespacePrefix);
     
    Rather than:
    XmlElem2 := XmlElement.Create('Header', namespaceUri);
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    19,307 Super User 2025 Season 2 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 3,750

#2
Sumit Singh Profile Picture

Sumit Singh 2,689

#3
YUN ZHU Profile Picture

YUN ZHU 1,987 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans