You will have different options, depending on what kind of 'Array' you are using. Is it an Array, List<>, or ArrayList?
For List<>, you can use CopyTo()
to grab parts of your List and put them into a binary array, which you could then write using XmlWriter. To read them back from the XmlReader, you can then simply use InsertRange
to de-serialize the data.
A Reading Example:
// elsewhere
List<byte> bytes;
// in the deserialization
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int index = 0;
int numRead = -1;
while (numRead != 0) // actually read stuff
{
numRead = reader.ReadContentAsBase64(buffer, bufferSize);
if (numRead > 0)
{
bytes.CopyTo(buffer, index, numRead);
index += numRead;
}
}
Note: code above is not tested, but is probably close. You can do something similar, but in reverse, for encoding and writing the data to base64. For other types, you simply need to convert your array into a list of bytes.
To get other types than bytes into a byte array, you'll need to use System.BitConverter
. This has two methods that will make you very happy: GetBytes
which converts any basic data type into a byte array, and ToXxx
, which includes ToInt32
and ToBoolean
. You'll be responsible for doing that conversion yourself after you've read in the base64 information or before you write it out.
You can use BitConverter to do the per-bit conversion to a set of bytes, but it's up to you to design an algorithm for converting your arrays to a single byte array and back.