XmlWriter.WriteBinHex(Byte[], Int32, Int32) 方法

定义

当在派生类中被重写时,将指定的二进制字节编码为 BinHex 并写出结果文本。

C#
public virtual void WriteBinHex (byte[] buffer, int index, int count);
C#
public abstract void WriteBinHex (byte[] buffer, int index, int count);

参数

buffer
Byte[]

要进行编码的字节数组。

index
Int32

缓冲区中指示要写入字节的起始位置的位置。

count
Int32

要写入的字节数。

例外

buffernull

编写器已关闭或处于错误状态。

  • 或 -

在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”

indexcount 小于零。

  • 或 - 缓冲区长度减去 index 小于 count

示例

以下示例使用 WriteBinHex 该方法写入 BinHex 数据。 数据 BinHex 嵌入在 <image> 元素中。

C#

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);
       writer.WriteStartDocument();
       writer.WriteStartElement("image");
       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);
       br.Close();

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

注解

有关此方法的异步版本,请参阅 WriteBinHexAsync

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0