AL icon indicating copy to clipboard operation
AL copied to clipboard

XML API - Replacement of Characters (XML Entities)

Open JoshC94 opened this issue 7 years ago • 7 comments

Hi,

I am currently trying to generate some XML using AL's XML API. While the generation itself works just fine, I am encountering an issue when it comes to format specific requirements.

The receiver of the XML file forces me to replace the chars ', ", <, >, & with their respective XML entities &apos;, &quot;, &lt;, &gt;, &amp;, regardless of their location in the document (Attribute or Textnode value). However XmlElement.SetAttribute() and XmlText.Create() only replace some of those chars in order to keep the document wellformed.

Attributes:

  • " --> &quot;
  • < --> &lt;
  • > --> &gt;
  • & --> &amp;

Textnodes:

  • < --> &lt;
  • > --> &gt;
  • & --> &amp;

Example

AL Code:

el := XmlElement.Create('greeting');
el.Add(XmlText.Create('''"Hello<>&'));
el.SetAttribute('lang', '''"en-US<>&');

Actual output:

<greeting lang="'&quot;en-US&lt;&gt;&amp;">
    '"Hello&lt;&gt;&amp;
</greeting>

Required output:

<greeting lang="&apos;&quot;en-US&lt;&gt;&amp;">
    &apos;&quot;Hello&lt;&gt;&amp;
</greeting>

So ' (&apos;) is never replaced, while " (&quot;) is only replaced when used in an attribute. Although this makes perfect sense, I need to replace all of the mentioned chars as per the requirements.

I tried to manually replace the chars with their respective XML entities before adding the text to an attribute / textnode, but, for example, instead of &apos; i got &amp;apos;, because Ampersands are replaced automatically when adding the preprocessed text. In the past (using C/AL and DotNet) we solved this by using XmlEntityReference, but as there currently is no such class in AL, I can't think of a possible solution for my problem.

Can anyone think of a solution for this? Or, if there currently is no way to accomplish what i am trying to do, would it be possible to add XmlEntityReference to AL's XML API?

Thanks in advance.

Kind Regards, Josh

JoshC94 avatar Jul 20 '18 08:07 JoshC94

Would it be feasible to build up the XML using the XML APIs but then before sending the document treat it as a regular string and do the final character replacement?

XmlDocument.WriteTo(outStream);
outStream.WriteText(text);
text.Replace('"', '&quot;');

StanislawStempin avatar Jul 24 '18 08:07 StanislawStempin

Hi,

thanks for your Answer. I thought of solving it in a similar way, but I realized I can't just replace every quote as doing so would destroy the XML structure (because attribute delimiters would also be replaced). It would therefore be necessary to find a context sensitive way of post processing, to only replace characters that are part of the actual content of attributes or text nodes.

Would it be possible to include XMLEntityReference (or something similar) into AL if there are no better options?

Josh

JoshC94 avatar Jul 24 '18 10:07 JoshC94

Would it be feasible to build up the XML using the XML APIs but then before sending the document treat it as a regular string and do the final character replacement?

XmlDocument.WriteTo(outStream);
outStream.WriteText(text);
text.Replace('"', '&quot;');

How to access each XML node one by one in AL? Please provide code if you have for reading nodes and their data one by one...

My XML Content [XmlText] snippet: <Access_Token> <Instance_Url>https://www.mywebsite.com/</Instance_Url> < YourToken>0_YdG/DRSVGCKyy6fx/fdasdY6avs=</ YourToken> <Expiration_date>05/28/2019 11:50:14 PM</Expiration_date> <Refresh_Token>0_i1G4fADRTVRDfdsac4GjD5R5UoOPks</Refresh_Token> </Access_Token>

Thanks in Advance!

TabrezAjaz avatar Oct 29 '18 02:10 TabrezAjaz

Hello, There may be some updates regarding this request? Currently there is no tool to influence the replacement of characters in XML. The option with replacing characters after creating the file is terrible. For example, we generate SAF-T files that take up more than 500MB, replacing will take a very long time and will take a lot of resources.

vlkov avatar Apr 01 '25 11:04 vlkov

Hi @vlkov ,

as described above, the AL XML APIs already handled the encoding of characters as needed to match the specification back when I posted this issue in 2018. However the problem was, that there were additional encoding requirements imposed by the particular third party XML API I was integrating. AFAIK, there are no such additional requirements for SAF-T. Do you have an example for your specific case?

JoshC94 avatar Apr 01 '25 16:04 JoshC94

Sure, but currently, we don't have a setter for the InnerXml property, as is available in the XmlElement class in C#. Additionally, when third-party programs may require escape of special characters like ", ', or > within XML node values, we don't have an integrated tool for escaping them. The only option is to replace these characters within the XML text. However, this approach is not straightforward because we need to replace only the text within node values without breaking the XML structure.

vlkov avatar Apr 04 '25 06:04 vlkov

Still relevant.

navdotnetreqs avatar Jun 23 '25 07:06 navdotnetreqs