XmlWriter.WriteBinHex(Byte[], Int32, Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当在派生类中被重写时,将指定的二进制字节编码为 BinHex
并写出结果文本。
public:
virtual void WriteBinHex(cli::array <System::Byte> ^ buffer, int index, int count);
public:
abstract void WriteBinHex(cli::array <System::Byte> ^ buffer, int index, int count);
public virtual void WriteBinHex (byte[] buffer, int index, int count);
public abstract void WriteBinHex (byte[] buffer, int index, int count);
abstract member WriteBinHex : byte[] * int * int -> unit
override this.WriteBinHex : byte[] * int * int -> unit
abstract member WriteBinHex : byte[] * int * int -> unit
Public Overridable Sub WriteBinHex (buffer As Byte(), index As Integer, count As Integer)
Public MustOverride Sub WriteBinHex (buffer As Byte(), index As Integer, count As Integer)
参数
- buffer
- Byte[]
要进行编码的字节数组。
- index
- Int32
缓冲区中指示要写入字节的起始位置的位置。
- count
- Int32
要写入的字节数。
例外
buffer
为 null
。
编写器已关闭或处于错误状态。
- 或 -
在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”
示例
以下示例使用 WriteBinHex 该方法写入 BinHex
数据。 数据 BinHex
嵌入在 <image>
元素中。
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();
}
}
Public Shared Sub BinHexEncodeImageFile()
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 BinHex data...")
Do
readBytes = br.Read(buffer, 0, bufferSize)
writer.WriteBinHex(buffer, 0, readBytes)
Loop While bufferSize <= readBytes
br.Close()
writer.WriteEndElement() ' </image>
writer.WriteEndDocument()
End Using
End Sub
注解
有关此方法的异步版本,请参阅 WriteBinHexAsync。