How do I validate strings to stop XmlDocument.ToString crashing app

If a user loads data into my app selecting an incorrect text encoding and then saves this data to an XML file my app can crash. Here is code to illustrate a crash:

=======
Dim s As String = “aáæ”
Dim s2 As String = ConvertEncoding(s, Encodings.MacRoman)

Dim doc as new XmlDocument
'Dim root as XmlNode = doc.CreateElement(s) // Works
Dim root as XmlNode = doc.CreateElement(s2) // Causes crash
doc.AppendChild root

Dim data as String = doc.ToString

=======

On macOS 10.15.7 I get a “AppName quit unexpectedly.” error when ‘doc.ToString’ is called. I see other XmlDocument.ToString issues in Feedback that haven’t been addressed so wondering if there’s a good way to validate the strings that are placed in the XML document to avoid this crash.

Thanks Mark

Xojo 2021r1.1

You need to convert your text to utf8. If you think your users really screw up then use CheckUTF8MBS from the MBS plugin.

Thanks Beatrix