This documentation is archived and is not being maintained.

XmlWriter.WriteBinHex Method

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

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

public virtual void WriteBinHex (
	byte[] buffer,
	int index,
	int count
public void WriteBinHex (
	byte[] buffer, 
	int index, 
	int count
public function WriteBinHex (
	buffer : byte[], 
	index : int, 
	count : int
Not applicable.



Byte array to encode.


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


The number of bytes to write.

Exception typeCondition


buffer is a null reference (Nothing in Visual Basic).


The buffer length minus index is less than count.


index or count is less than zero.

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

public static void BinHexEncodeImageFile() {

  int bufferSize = 1000;
  byte[] buffer = new byte[bufferSize];
  int readBytes = 0;
  using (XmlWriter writer = XmlWriter.Create("output.xml")) {
       FileStream inputFile = new FileStream(@"C:\artFiles\sunset.jpg", FileMode.OpenOrCreate, 
                                                                    FileAccess.Read, FileShare.Read);
       BinaryReader br = new BinaryReader(inputFile);
       Console.WriteLine("\r\nWriting BinHex data...");

       do {
          readBytes = br.Read(buffer, 0, bufferSize);
          writer.WriteBinHex(buffer, 0, readBytes);
       } while (bufferSize <= readBytes);

    writer.WriteEndElement();// </image>

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0