XmlReader.ReadElementContentAsBinHex(Byte[], Int32, Int32) 方法


读取元素并对 BinHex 内容进行解码。Reads the element and decodes the BinHex content.

 virtual int ReadElementContentAsBinHex(cli::array <System::Byte> ^ buffer, int index, int count);
public virtual int ReadElementContentAsBinHex (byte[] buffer, int index, int count);
abstract member ReadElementContentAsBinHex : byte[] * int * int -> int
override this.ReadElementContentAsBinHex : byte[] * int * int -> int
Public Overridable Function ReadElementContentAsBinHex (buffer As Byte(), index As Integer, count As Integer) As Integer



结果文本复制到的缓冲区。The buffer into which to copy the resulting text. 此值不能为 nullThis value cannot be null.


缓冲区中的偏移,从这个位置开始将结果复制到缓冲区中。The offset into the buffer where to start copying the result.


要复制到缓冲区的最大字节数。The maximum number of bytes to copy into the buffer. 此方法返回复制的实际字节数。The actual number of bytes copied is returned from this method.



写入缓冲区的字节数。The number of bytes written to the buffer.


buffer 值为 nullThe buffer value is null.

当前节点不是元素节点。The current node is not an element node.

- 或 --or-

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

缓冲区中的索引或者索引与计数之和大于分配的缓冲区大小。The index into the buffer or index + count is larger than the allocated buffer size.

XmlReader 实现不支持此方法。The XmlReader implementation does not support this method.

该元素包含混合内容。The element contains mixed-content.

无法将内容转换成请求的类型。The content cannot be converted to the requested type.


下面的示例读取内联 BinHex 编码的图像。The following example reads an inline BinHex encoded image. BinHex数据嵌入到 <image> 元素中。The BinHex data is embedded within the <image> element. BinaryWriter用于创建新的二进制数据文件。A BinaryWriter is used to create a new binary data file.

public static void BinHexDecodeImageFile() {

  byte[] buffer = new byte[1000];
  int readBytes = 0;

  using (XmlReader reader = XmlReader.Create("output.xml")) {
        FileStream outputFile = new FileStream(@"C:\artFiles\data\newImage.jpg", FileMode.OpenOrCreate,
                                                                      FileAccess.Write, FileShare.Write);
        // Read to the image element.
        // Read the BinHex data.
        Console.WriteLine("\r\nReading BinHex...");
        BinaryWriter bw = new BinaryWriter(outputFile);
        while ((readBytes = reader.ReadElementContentAsBinHex(buffer, 0, 50))>0) {
            bw.Write(buffer, 0, readBytes);
Public Shared Sub BinHexDecodeImageFile() 
    Dim buffer(999) As Byte
    Dim readBytes As Integer = 0
    Using reader As XmlReader = XmlReader.Create("output.xml")
            Dim outputFile As New FileStream("C:\artFiles\data\newImage.jpg", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)
            ' Read to the image element.
            ' Read the BinHex data.
            Console.WriteLine(vbCr + vbLf + "Reading BinHex...")
            Dim bw As New BinaryWriter(outputFile)
            readBytes = reader.ReadElementContentAsBinHex(buffer, 0, 50)
            While (readBytes > 0)
                bw.Write(buffer, 0, readBytes)
                readBytes = reader.ReadElementContentAsBinHex(buffer, 0, 50)
            End While
    End Using

End Sub


此方法读取元素内容,使用编码对其进行解码, BinHex 并返回解码的二进制字节 (例如,将内联 BinHex 编码的 GIF 图像) 到缓冲区中。This method reads the element content, decodes it using BinHex encoding, and returns the decoded binary bytes (for example, an inline BinHex-encoded GIF image) into the buffer.

此方法只能读取简单内容元素。This method can only read simple-content elements. 元素可以包含文本、空格、有效空白、CDATA 部分、注释和处理指令。The element can contain text, white space, significant white space, CDATA sections, comments and processing instructions. 它还可以包含自动扩展的实体引用。It can also contain entity references, which are automatically expanded. 元素不能有子元素。The element cannot have child elements.

此方法与方法非常类似, ReadContentAsBinHex 只是它只能在元素节点类型上调用。This method is very similar to the ReadContentAsBinHex method except that it can only be called on element node types.

如果 count 值大于文档中的字节数,或者它等于文档中的字节数,则 XmlReader 读取文档中的所有剩余字节并返回读取的字节数。If the count value is higher than the number of bytes in the document, or if it is equal to the number of bytes in the document, the XmlReader reads all the remaining bytes in the document and returns the number of bytes read. 下一个 XmlReader 方法调用返回零,并将读取器移动到后面的节点 EndElementThe next XmlReader method call returns a zero and moves the reader to the node following the EndElement.

如果在 Read 所有元素内容被使用之前调用,则读取器的行为可能与先使用第一个内容,然后 Read 调用方法相同。If you call Read before all of the element content is consumed, the reader may behave as if the first content was consumed and then the Read method was called. 这意味着读取器将读取所有文本,直到遇到结束元素。This means that the reader will read all the text until the end element is encountered. 然后,它将读取结束标记节点,读取下一个节点,然后在下一个后续节点上定位自身。It will then read the end tag node, read the next node, and then position itself on the next subsequent node.

有关此方法的异步版本,请参阅 ReadElementContentAsBinHexAsyncFor the asynchronous version of this method, see ReadElementContentAsBinHexAsync.