Convert Memory Block data to XML

Hello,

How do I convert data from a MutableMemoryBlock so that it can be placed in an XML node? I will eventually send this XML document through a socket.

Here is my code that throws an error when appending data to the child root node:

[code]Sub Action() Handles Action
Dim mb as New Xojo.Core.MutableMemoryBlock(4)
mb.UInt16Value(0) = 34
mb.UInt16Value(2) = 66

Dim xml as New XmlDocument

Dim RootNode as XmlNode
RootNode = xml.AppendChild(xml.CreateElement(“Data”))

RootNode.AppendChild(xml.CreateTextNode(Xojo.Core.TextEncoding.UTF32LittleEndian.ConvertDataToText(mb))) //Error

System.DebugLog xml.ToString
End Sub
[/code]

The final XML would likely look something like this:

<?xml version="1.0" encoding="UTF-8"?> <Data>Zp</Data>

I am not able to figure out how to convert the mutable memory block data to a string that can be added to XML.

Thanks for your help.

What kind of error?

The error message is “Encountered invalid character”

Maybe XML only is able to work with UTF-8? I am not sure.

there are some entities in XML that have to be encoded as they are used to define the XML itself
https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

and if you want to use them IN the data they must use the entity &, >, < &gt, ", '

heh
you stuff 16 bit values in and try to convert using utf32 ???
the data is UTF16 not UTF32 :slight_smile:

Dim mb As New Xojo.Core.MutableMemoryBlock(4)
mb.UInt16Value(0) = 34 // you stuff 16 bit values in here
mb.UInt16Value(2) = 66

Dim xml As New XmlDocument

Dim RootNode As XmlNode
RootNode = xml.AppendChild(xml.CreateElement("Data"))

Dim t As Text = Xojo.Core.TextEncoding.UTF16LittleEndian.ConvertDataToText(mb) // <<<< :P not UTF32
RootNode.AppendChild(xml.CreateTextNode(t)) //Error 

System.DebugLog xml.ToString

Thank you Norman!!

I made the poor assumption that I could just stuff the memory block with data (UInt16’s) and then convert it with a UInt32 (because its 2 x 16 bit)… A poor decision on my part :slight_smile:

Thank you for your help, as this makes sense.

You may as long as what you stuff in IS valid UTF32 (which in this case it seems not to be)

@Eugene Dakin
Convert data which your transporting to base64 or HEX kind of data and then put it in XML node. On 2nd end, just decode it and make of it mb.

TIP:
Do a testing of encoding/decoding with base64 since on some supported dev. platforms Xojo doesn’t handle it well.
HEX encoding is also okay but it’s bigger in data size for transporting.

@Bogdan Pavlovic
Thank you for the good suggestion. I will experiment with the use of converting data to hexadecimal and back.