XmlTextReader.ReadBase64 - MSDN Example
I'm not sure if I'm doing something wrong, reading wrong, or just wrote some kludge, but just spent some time trying to get the MSDN example on .ReadBase64 working. Does anyone concur that the example is incorrect? Or is it my bad?
In particular, here's the code they use in a loop to read:
Code:
base64len = reader.ReadBase64(base64, 0, 50);
I changed to
Code:
base64len += reader.ReadBase64(base64, base64len, 50);
Seems to me the MSDN example would read the next 50 bytes and always overwrite the buffer beginning at position zero. Or is it just me?
Re: XmlTextReader.ReadBase64 - MSDN Example
Well, I don't even think I care anymore. Does anybody even use .ReadBase64? All of this code
Code:
int base64len = 0;
// 1048576 bytes = one meg.
byte[] base64 = new byte[1048576];
do
{
base64len += reader.ReadBase64(base64, base64len, 50);
}
while (reader.Name.Equals("Mug"));
si.Mug = base64;
seems to be able to be replaced by this
Code:
reader.Read();
string base64 = reader.Value;
byte[] decoded = Convert.FromBase64String(base64);
si.Mug = decoded;
It's cleaner, easier to read, and you don't have to declare the size of the byte array up front. I guess that's a better way to extract your base64 encoded binary file from xml?