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

定義

讀取項目,並將 Base64 內容解碼。

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[]

將產生的文字複製到其中的緩衝區。 這個值不能是 null

index
Int32

緩衝區中開始複製結果的位移。

count
Int32

要複製至緩衝區中的最大位元組數目。 從這個方法傳回所複製的實際位元組數目。

傳回

寫入緩衝區的位元組數目。

例外狀況

buffer 值為 null

目前的節點不是項目節點。

-或-

在先前的非同步作業完成前呼叫了 XmlReader 方法。 在此情況下,會擲回 InvalidOperationException 與「非同步作業已經在進行中」的訊息。

緩衝區的索引或是索引 + 計數大於所配置的緩衝區大小。

XmlReader 實作不支援這個方法。

項目包含混合內容。

內容無法轉換成要求的類型。

範例

下列範例會讀取內嵌 Base64 編碼影像。 資料 Base64 內嵌在 元素內 <image>BinaryWriter用來建立新的二進位資料檔案。


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

備註

這個方法會讀取專案內容、使用 Base64 編碼將它解碼,並將解碼的二進位位元組傳回 (例如,內嵌 Base64 編碼 GIF 影像) 緩衝區。 如需詳細資訊,請參閱 RFC 1521「MIME (多用途網際網路郵件延伸模組) 第一部分:指定及描述網際網路訊息本文格式的機制」。 您可以從 要求批註網站取得 RFC。

ReadElementContentAsBase64 只能讀取簡單內容專案。 元素可以包含文字、空白字元、重大空白字元、CDATA 區段、批註和處理指示。 它也可以包含會自動展開的實體參考。 專案不能有子專案。

這個方法與 方法非常類似 ReadContentAsBase64 ,不同之處在于它只能在元素節點類型上呼叫。

count如果值高於檔中的位元組數目,或等於檔中的位元組數目,則會 XmlReader 讀取檔中的所有剩餘位元組,並傳回讀取的位元組數。 下一 XmlReader 個方法呼叫會傳回零,並將讀取器移至 後面的 EndElement 節點。

如果您在取用所有元素內容之前呼叫 Read ,則讀取器的行為可能會像取用第一個內容,然後 Read 呼叫 方法一樣。 這表示讀取器會讀取所有文字,直到遇到結束專案為止。 接著,它會讀取結束標籤節點、讀取下一個節點,然後將本身放在下一個後續節點上。

如需這個方法的非同步版本,請參閱 ReadElementContentAsBase64Async

適用於

另請參閱