XmlReader.ReadElementContentAsBase64(Byte[], Int32, Int32) XmlReader.ReadElementContentAsBase64(Byte[], Int32, Int32) XmlReader.ReadElementContentAsBase64(Byte[], Int32, Int32) XmlReader.ReadElementContentAsBase64(Byte[], Int32, Int32) Method

定义

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

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

参数

buffer
Byte[]

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

index
Int32 Int32 Int32 Int32

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

count
Int32 Int32 Int32 Int32

要复制到缓冲区的最大字节数。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.

异常

当前节点不是元素节点。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.

示例

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


public static void Base64DecodeImageFile() {

  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.
        reader.ReadToFollowing("image");
        // Read the Base64 data.
        Console.WriteLine("\r\nReading Base64...");
        BinaryWriter bw = new BinaryWriter(outputFile);
        while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, 50))>0) {
            bw.Write(buffer, 0, readBytes);
        }
        outputFile.Close();
  }
}
Public Shared Sub Base64DecodeImageFile() 
    
    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.
            reader.ReadToFollowing("image")
            ' Read the Base64 data.
            Console.WriteLine(vbCr + vbLf + "Reading Base64...")
            Dim bw As New BinaryWriter(outputFile)
            readBytes = reader.ReadElementContentAsBase64(buffer, 0, 50)
            While (readBytes > 0)
                bw.Write(buffer, 0, readBytes)
                readBytes = reader.ReadElementContentAsBase64(buffer, 0, 50)
            End While
            outputFile.Close()
        
    End Using

End Sub 'Base64DecodeImageFile

注解

此方法读取元素内容中,将使用其解码Base64编码,并返回已解码的二进制字节 (例如,内联Base64-编码的 GIF 图像) 到缓冲区。This method reads the element content, decodes it using Base64 encoding, and returns the decoded binary bytes (for example, an inline Base64-encoded GIF image) into the buffer. 有关详细信息,请参阅 RFC 1521"MIME (多用途 Internet 邮件扩展) 部分一个:用于指定和 Internet 消息正文的格式描述机制"。For more information, see RFC 1521, "MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies". 您可以获得从 Rfc征求意见文档网站You can obtain RFCs from the Request for Comments Web site.

ReadElementContentAsBase64 可仅读取简单内容的元素。ReadElementContentAsBase64 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.

此方法是非常类似于ReadContentAsBase64方法,但它只能在元素节点类型上调用。This method is very similar to the ReadContentAsBase64 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方法调用会返回 0 并将读取器移动到节点以下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.

此方法的异步版本,请参阅ReadElementContentAsBase64AsyncFor the asynchronous version of this method, see ReadElementContentAsBase64Async.

适用于

另请参阅