Encodes the specified binary bytes as base64 and writes out the resulting text.
Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)
Visual Basic (Declaration)
Public Overrides Sub WriteBase64 ( _
buffer As Byte(), _
index As Integer, _
count As Integer _
)
Dim instance As XmlTextWriter
Dim buffer As Byte()
Dim index As Integer
Dim count As Integer
instance.WriteBase64(buffer, index, count)
public override void WriteBase64 (
byte[] buffer,
int index,
int count
)
public:
virtual void WriteBase64 (
array<unsigned char>^ buffer,
int index,
int count
) override
public void WriteBase64 (
byte[] buffer,
int index,
int count
)
public override function WriteBase64 (
buffer : byte[],
index : int,
count : int
)
Parameters
- buffer
Byte array to encode.
- index
The position within the buffer indicating the start of the bytes to write.
- count
The number of bytes to write.
The following example encodes an input file using WriteBase64 and generates a temporary XML file. The temporary XML file is decoded using the ReadBase64 method and compared to the original file.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Text
class TestBase64
private const bufferSize as integer = 4096
public shared sub Main()
Dim args() As String = System.Environment.GetCommandLineArgs()
Dim myTestBase64 as TestBase64 = new TestBase64()
'Check that the usage string is correct.
if (args.Length < 3 )
myTestBase64.Usage()
return
end if
Dim fileOld as FileStream = new FileStream(args(1), FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
myTestBase64.EncodeXmlFile("temp.xml", fileOld)
Dim fileNew as FileStream = new FileStream(args(2), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
myTestBase64.DecodeOrignalObject("temp.xml", fileNew)
'Compare the two files.
if (myTestBase64.CompareResult(fileOld, fileNew))
Console.WriteLine("The recreated binary file {0} is the same as {1}", args(2), args(1))
else
Console.WriteLine("The recreated binary file {0} is not the same as {1}", args(2), args(1))
end if
fileOld.Flush()
fileNew.Flush()
fileOld.Close()
fileNew.Close()
end sub
'Use the WriteBase64 method to create an XML document. The object
'passed by the user is encoded and included in the document.
public shared sub EncodeXmlFile(xmlFileName as String, fileOld as FileStream)
Dim buffer(bufferSize) as byte
Dim readByte as integer=0
Dim xw as XmlTextWriter = new XmlTextWriter(xmlFileName, Encoding.UTF8)
xw.WriteStartDocument()
xw.WriteStartElement("root")
' Create a Char writer.
Dim br as BinaryReader = new BinaryReader(fileOld)
' Set the file pointer to the end.
try
do
readByte=br.Read(buffer, 0, bufferSize)
xw.WriteBase64(buffer, 0, readByte)
loop while (bufferSize <= readByte )
catch ex as Exception
Dim ex1 as EndOfStreamException = new EndOfStreamException()
if (ex1.Equals(ex))
Console.WriteLine("We are at end of file")
else
Console.WriteLine(ex)
end if
end try
xw.WriteEndElement()
xw.WriteEndDocument()
xw.Flush()
xw.Close()
end sub
'Use the ReadBase64 method to decode the new XML document
'and generate the original object.
public shared sub DecodeOrignalObject(xmlFileName as String, fileNew as FileStream)
Dim buffer(bufferSize) as byte
Dim readByte as integer =0
'Create a file to write the bmp back.
Dim bw as BinaryWriter = new BinaryWriter(fileNew)
Dim tr as XmlTextReader = new XmlTextReader(xmlFileName)
tr.MoveToContent()
Console.WriteLine(tr.Name)
do
readByte=tr.ReadBase64(buffer, 0, bufferSize)
bw.Write(buffer, 0, readByte)
loop while(readByte>=bufferSize)
bw.Flush()
end sub
'Compare the two files.
public function CompareResult(fileOld as FileStream, fileNew as FileStream) as boolean
Dim readByteOld as integer=0
Dim readByteNew as integer=0
Dim count as integer
Dim readByte as integer=0
Dim bufferOld(bufferSize) as byte
Dim bufferNew(bufferSize) as byte
Dim binaryReaderOld as BinaryReader = new BinaryReader(fileOld)
Dim binaryReaderNew as BinaryReader = new BinaryReader(fileNew)
binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin)
binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin)
do
readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize)
readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize)
if not (readByteOld=readByteNew)
return false
end if
for count=0 to bufferSize-1
if not (bufferOld(count)=bufferNew(count))
return false
end if
next
loop while (count<readByte)
return true
end function
'Display the usage statement.
public shared sub Usage()
Console.WriteLine("TestBase64 sourceFile, targetFile")
Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp")
end sub
end class
using System;
using System.IO;
using System.Xml;
using System.Text;
class TestBase64 {
private const int bufferSize=4096;
public static void Main(String[] args) {
TestBase64 testBase64=new TestBase64();
//Check that the usage string is correct.
if (args.Length < 2 ) {
testBase64.Usage();
return;
}
FileStream fileOld = new FileStream(args[0], FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
testBase64.EncodeXmlFile("temp.xml", fileOld);
FileStream fileNew = new FileStream(args[1], FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
testBase64.DecodeOrignalObject("temp.xml", fileNew);
//Compare the two files.
if (testBase64.CompareResult( fileOld, fileNew)) {
Console.WriteLine("The recreated binary file {0} is the same as {1}", args[1], args[0] );
} else {
Console.WriteLine("The recreated binary file {0} is not the same as {1}", args[1], args[0] );
}
fileOld.Flush();
fileNew.Flush();
fileOld.Close();
fileNew.Close();
}
//Use the WriteBase64 method to create an XML document. The object
//passed by the user is encoded and included in the document.
public void EncodeXmlFile(String xmlFileName, FileStream fileOld) {
byte[] buffer = new byte[bufferSize];
int readByte=0;
XmlTextWriter xw = new XmlTextWriter(xmlFileName, Encoding.UTF8);
xw.WriteStartDocument();
xw.WriteStartElement("root");
// Create a Char writer.
BinaryReader br = new BinaryReader(fileOld);
// Set the file pointer to the end.
try {
do {
readByte=br.Read(buffer, 0, bufferSize);
xw.WriteBase64(buffer, 0, readByte);
} while (bufferSize <= readByte );
} catch (Exception ex) {
EndOfStreamException ex1= new EndOfStreamException();
if (ex1.Equals(ex)) {
Console.WriteLine("We are at end of file");
} else {
Console.WriteLine(ex);
}
}
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Flush();
xw.Close();
}
//Use the ReadBase64 method to decode the new XML document
//and generate the original object.
public void DecodeOrignalObject(String xmlFileName, FileStream fileNew) {
byte[] buffer = new byte[bufferSize];
int readByte=0;
//Create a file to write the bmp back.
BinaryWriter bw = new BinaryWriter(fileNew);
XmlTextReader tr = new XmlTextReader(xmlFileName);
tr.MoveToContent();
Console.WriteLine(tr.Name);
do {
readByte=tr.ReadBase64(buffer, 0, bufferSize);
bw.Write(buffer, 0, readByte);
} while(readByte>=bufferSize);
bw.Flush();
}
//Compare the two files.
public bool CompareResult(FileStream fileOld, FileStream fileNew) {
int readByteOld=0;
int readByteNew=0;
int count, readByte=0;
byte[] bufferOld = new byte[bufferSize];
byte[] bufferNew = new byte[bufferSize];
BinaryReader binaryReaderOld = new BinaryReader(fileOld);
BinaryReader binaryReaderNew = new BinaryReader(fileNew);
binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin);
binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin);
do {
readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize);
readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize);
if (readByteOld!=readByteNew) {
return false;
}
for (count=0; count <bufferSize; ++count) {
if (bufferOld[count]!=bufferNew[count]) {
return false;
}
}
} while (count<readByte );
return true;
}
//Display the usage statement.
public void Usage() {
Console.WriteLine("TestBase64 sourceFile, targetFile \n");
Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp\n");
}
}
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Text;
ref class TestBase64
{
private:
static int bufferSize = 4096;
public:
// Use the WriteBase64 method to create an XML document. The object
// passed by the user is encoded and included in the document.
void EncodeXmlFile( String^ xmlFileName, FileStream^ fileOld )
{
array<Byte>^buffer = gcnew array<Byte>(bufferSize);
int readByte = 0;
XmlTextWriter^ xw = gcnew XmlTextWriter( xmlFileName,Encoding::UTF8 );
xw->WriteStartDocument();
xw->WriteStartElement( "root" );
// Create a Char writer.
BinaryReader^ br = gcnew BinaryReader( fileOld );
// Set the file pointer to the end.
try
{
do
{
readByte = br->Read( buffer, 0, bufferSize );
xw->WriteBase64( buffer, 0, readByte );
}
while ( bufferSize <= readByte );
}
catch ( Exception^ ex )
{
EndOfStreamException^ ex1 = gcnew EndOfStreamException;
if ( ex1->Equals( ex ) )
Console::WriteLine( "We are at end of file" );
else
Console::WriteLine( ex );
}
xw->WriteEndElement();
xw->WriteEndDocument();
xw->Flush();
xw->Close();
}
// Use the ReadBase64 method to decode the new XML document
// and generate the original object.
void DecodeOrignalObject( String^ xmlFileName, FileStream^ fileNew )
{
array<Byte>^buffer = gcnew array<Byte>(bufferSize);
int readByte = 0;
// Create a file to write the bmp back.
BinaryWriter^ bw = gcnew BinaryWriter( fileNew );
XmlTextReader^ tr = gcnew XmlTextReader( xmlFileName );
tr->MoveToContent();
Console::WriteLine( tr->Name );
do
{
readByte = tr->ReadBase64( buffer, 0, bufferSize );
bw->Write( buffer, 0, readByte );
}
while ( readByte >= bufferSize );
bw->Flush();
}
// Compare the two files.
bool CompareResult( FileStream^ fileOld, FileStream^ fileNew )
{
int readByteOld = 0;
int readByteNew = 0;
int count;
int readByte = 0;
array<Byte>^bufferOld = gcnew array<Byte>(bufferSize);
array<Byte>^bufferNew = gcnew array<Byte>(bufferSize);
BinaryReader^ binaryReaderOld = gcnew BinaryReader( fileOld );
BinaryReader^ binaryReaderNew = gcnew BinaryReader( fileNew );
binaryReaderOld->BaseStream->Seek( 0, SeekOrigin::Begin );
binaryReaderNew->BaseStream->Seek( 0, SeekOrigin::Begin );
do
{
readByteOld = binaryReaderOld->Read( bufferOld, 0, bufferSize );
readByteNew = binaryReaderNew->Read( bufferNew, 0, bufferSize );
if ( readByteOld != readByteNew )
return false;
for ( count = 0; count < bufferSize; ++count )
if ( bufferOld[ count ] != bufferNew[ count ] )
return false;
}
while ( count < readByte );
return true;
}
// Display the usage statement.
void Usage()
{
Console::WriteLine( "TestBase64 sourceFile, targetFile \n" );
Console::WriteLine( "For example: TestBase64 winlogon.bmp, target.bmp\n" );
}
};
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
TestBase64^ testBase64 = gcnew TestBase64;
// Check that the usage is correct.
if ( args->Length < 3 )
{
testBase64->Usage();
return 1;
}
FileStream^ fileOld = gcnew FileStream( args[ 1 ],FileMode::OpenOrCreate,FileAccess::Read,FileShare::Read );
testBase64->EncodeXmlFile( "temp.xml", fileOld );
FileStream^ fileNew = gcnew FileStream( args[ 2 ],FileMode::Create,FileAccess::ReadWrite,FileShare::ReadWrite );
testBase64->DecodeOrignalObject( "temp.xml", fileNew );
// Compare the two files.
if ( testBase64->CompareResult( fileOld, fileNew ) )
Console::WriteLine( "The recreated binary file {0} is the same as {1}", args[ 2 ], args[ 1 ] );
else
Console::WriteLine( "The recreated binary file {0} is not the same as {1}", args[ 2 ], args[ 1 ] );
fileOld->Flush();
fileNew->Flush();
fileOld->Close();
fileNew->Close();
return 0;
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Text.*;
class TestBase64
{
private int bufferSize = 4096;
public static void main(String[] args)
{
TestBase64 testBase64 = new TestBase64();
//Check that the usage string is correct.
if (args.get_Length() < 2) {
testBase64.Usage();
return;
}
FileStream fileOld = new FileStream(args[0], FileMode.OpenOrCreate,
FileAccess.Read, FileShare.Read);
testBase64.EncodeXmlFile("temp.xml", fileOld);
FileStream fileNew = new FileStream(args[1], FileMode.Create,
FileAccess.ReadWrite, FileShare.ReadWrite);
testBase64.DecodeOrignalObject("temp.xml", fileNew);
//Compare the two files.
if (testBase64.CompareResult(fileOld, fileNew)) {
Console.WriteLine("The recreated binary file {0} is the same as {1}",
args[1], args[0]);
}
else {
Console.WriteLine("The recreated binary file {0} is not the"
+ " same as {1}", args[1], args[0]);
}
fileOld.Flush();
fileNew.Flush();
fileOld.Close();
fileNew.Close();
} //main
//Use the WriteBase64 method to create an XML document. The object
//passed by the user is encoded and included in the document.
public void EncodeXmlFile(String xmlFileName, FileStream fileOld)
{
ubyte buffer[] = new ubyte[bufferSize];
int readByte = 0;
XmlTextWriter xw = new XmlTextWriter(xmlFileName, Encoding.get_UTF8());
xw.WriteStartDocument();
xw.WriteStartElement("root");
// Create a Char writer.
BinaryReader br = new BinaryReader(fileOld);
// Set the file pointer to the end.
try {
do {
readByte = br.Read(buffer, 0, bufferSize);
xw.WriteBase64(buffer, 0, readByte);
} while (bufferSize <= readByte);
}
catch (System.Exception ex) {
EndOfStreamException ex1 = new EndOfStreamException();
if (ex1.Equals(ex)) {
Console.WriteLine("We are at end of file");
}
else {
Console.WriteLine(ex);
}
}
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Flush();
xw.Close();
} //EncodeXmlFile
//Use the ReadBase64 method to decode the new XML document
//and generate the original object.
public void DecodeOrignalObject(String xmlFileName, FileStream fileNew)
{
ubyte buffer[] = new ubyte[bufferSize];
int readByte = 0;
//Create a file to write the bmp back.
BinaryWriter bw = new BinaryWriter(fileNew);
XmlTextReader tr = new XmlTextReader(xmlFileName);
tr.MoveToContent();
Console.WriteLine(tr.get_Name());
do {
readByte = tr.ReadBase64(buffer, 0, bufferSize);
bw.Write(buffer, 0, readByte);
} while (readByte >= bufferSize);
bw.Flush();
} //DecodeOrignalObject
//Compare the two files.
public boolean CompareResult(FileStream fileOld, FileStream fileNew)
{
int readByteOld = 0;
int readByteNew = 0;
int readByte = 0;
int count;
ubyte bufferOld[] = new ubyte[bufferSize];
ubyte bufferNew[] = new ubyte[bufferSize];
BinaryReader binaryReaderOld = new BinaryReader(fileOld);
BinaryReader binaryReaderNew = new BinaryReader(fileNew);
binaryReaderOld.get_BaseStream().Seek(0, SeekOrigin.Begin);
binaryReaderNew.get_BaseStream().Seek(0, SeekOrigin.Begin);
do {
readByteOld = binaryReaderOld.Read(bufferOld, 0, bufferSize);
readByteNew = binaryReaderNew.Read(bufferNew, 0, bufferSize);
if (readByteOld != readByteNew) {
return false;
}
for (count = 0; count < bufferSize; ++count) {
if (bufferOld.get_Item(count) != bufferNew.get_Item(count)) {
return false;
}
}
} while (count < readByte);
return true;
} //CompareResult
//Display the usage statement.
public void Usage()
{
Console.WriteLine("TestBase64 sourceFile, targetFile \n");
Console.WriteLine("For example: TestBase64 winlogon.bmp, target.bmp\n");
} //Usage
} //TestBase64
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 2.0, 1.0