XmlWriter.WriteBase64 Method (Byte(), Int32, Int32)

 

When overridden in a derived class, encodes the specified binary bytes as Base64 and writes out the resulting text.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

Public MustOverride Sub WriteBase64 (
	buffer As Byte(),
	index As Integer,
	count As Integer
)

Parameters

buffer
Type: System.Byte()

Byte array to encode.

index
Type: System.Int32

The position in the buffer indicating the start of the bytes to write.

count
Type: System.Int32

The number of bytes to write.

Exception Condition
ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

index or count is less than zero.

-or-

The buffer length minus index is less than count.

InvalidOperationException

An XmlWriter method was called before a previous asynchronous operation finished. In this case, InvalidOperationException is thrown with the message “An asynchronous operation is already in progress.”

For example, the byte buffer may contain the binary contents of a GIF image. This clearly would not be valid XML. The Base64 encoding is designed to represent arbitrary byte sequences in a text form comprised of the 65 US-ASCII characters ([A-Za-z0-9+/=]) where each character encodes 6 bits of the binary data. For more information, see Requests for Comments (RFC) 1521 located on the Request for Comments Web site at http://www.rfc-editor.org/.

For the asynchronous version of this method, see WriteBase64Async.

The following example uses the WriteBase64 method to write Base64 data. The Base64 data is embedded within an <image> element.

Public Shared Sub Base64EncodeImageFile() 

    Dim bufferSize As Integer = 1000
    Dim buffer(bufferSize) As Byte
    Dim readBytes As Integer = 0

    Using writer As XmlWriter = XmlWriter.Create("output.xml")

            Dim inputFile As New FileStream("C:\artFiles\sunset.jpg", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)
            writer.WriteStartDocument()
            writer.WriteStartElement("image")
            Dim br As New BinaryReader(inputFile)
            Console.WriteLine(vbCr + vbLf + "Writing Base64 data...")

            Do
                readBytes = br.Read(buffer, 0, bufferSize)
                writer.WriteBase64(buffer, 0, readBytes)
            Loop While bufferSize <= readBytes
            br.Close()

        writer.WriteEndElement() ' </image>
        writer.WriteEndDocument()

    End Using

End Sub 'Base64EncodeImageFile

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: