String to XML (escape special characters)

Without creating an XMLDocument and adding element tags etc, is it possible to parse a string and convert it to XML-friendly format by escaping any special characters e.g. ampersand etc intended for use as the content value between XML tags?

I was hoping there’s a built-in method I could use but can’t find anything.

If I have to create an XMLDocument, how can I add element content between element tags? All example below do not show any content values between tags:

XMLDocument — Xojo documentation

There is an entry named “Escaping” there:

The XMLDocument API is a bit verbose, but it’s the right way to generate XML.
You should file a ticket for better documentation.

This Xojo code will generate a single XML node in the shape you’re looking for.

var xmlDoc as new XMLDocument
var xmlBody as XMLNode = xmlDoc.DocumentElement

var xmlThis as XMLNode = xmlDoc.CreateElement("This")
var xmlValue as XmlTextNode = xmlDoc.CreateTextNode("Value")

xmlThis.AppendChild(xmlValue)

xmlBody.AppendChild(xmlThis)

var s as String = xmlDoc.ToString
break
<?xml version="1.0" encoding="UTF-8"?>
<This>Value</This>
4 Likes

To demonstrate the other part you were asking, change the word “Value” to “This & That” and you see that the result of what @Tim_Parnell was saying about using the XMLDocument API being the right way to generate XML. It deals with the special characters automatically.

<?xml version="1.0" encoding="UTF-8"?>
<This>This &amp; That</This>
1 Like