Marty131
Marty131

Reputation: 345

Azure API Policy Find And Replace - from "<" to "<"

Within the Azure API Management, I have an API, within its outbound policy I wish to find-and-replace like this;

<find-and-replace from="&lt;" to="<" />

However, the "<" character is of course illegal since the policy itself is written in XML giving me the following error;

Error parsing policy xml document. '<', hexadecimal value 0x3C, is an invalid attribute character. Line 72, position 43.

Reason:

My backend API returns a text string which I want to "convert" into a valid XML.

Questions:

  1. Would a string that uses & gt ; and & lt ; (minus the spaces) instead of < and > still be XML valid?
  2. If the answer to #1 is no, then, how can I find and replace within the Azure API policy?

Upvotes: 1

Views: 2945

Answers (1)

Vitaliy Kurokhtin
Vitaliy Kurokhtin

Reputation: 7810

Since you're want to convert "<" (that is three separate characters) to "<" AND you're writing an XML you have to double encode things. Try:

<find-and-replace from="&amp;lt;" to="&lt;" />

This way XML after XML decoding first string will become "&lt;", and second - "<".

Note that this is not APIM problem, it's an XML encoding problem, for example this code:

new XElement("el", new XAttribute("at", "&lt;")).ToString()

produces such XML:

<el at="&amp;lt;" />

Upvotes: 3

Related Questions