Sam Drucker
Sam Drucker

Reputation: 25

How do I serialize binary data without the XML metatags?

I am wanting to obfuscate some binary or text data that is in a binary array byte[], I am able to accomplish converting into the string I want, but the XmlSerializer output includes the extra information. Is there a different serializer that could be used to convert my data without having the XML tags appended? If I need to deserialize it later, I will include the necessary tags.

byte[] baTest = new byte[256];
        for (int i = 0; i < 256; i++)
            baTest[i] = (byte) (i & 0xff);
        string MyTestString = ReadableXMLBinary(baTest);
        MessageBox.Show("My String is:\n" + MyTestString);

static public string ReadableXMLBinary(byte[] baIn)
        {
            StringWriter s = new StringWriter();
            XmlSerializer xser = new XmlSerializer(typeof( byte[]));
            xser.Serialize(s, baIn);
            return s.ToString();
        }

Which outputs:

<?xml version="1.0" encoding="utf-16"?><base64Binary>AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==</base64Binary>"

Basically, I want everything between the <base64Binary> tags. I know I can strip out the text inside with little effort, but I thought there might be a clean solution that someone may suggest.

Upvotes: 0

Views: 288

Answers (1)

Thoryn Hawley
Thoryn Hawley

Reputation: 363

It looks like you don't want XML at all?

If that is the case, then use: string s = Convert.ToBase64String(baIn);

and to convert back: byte[] baOut = Convert.FromBase64String(s);

Upvotes: 1

Related Questions