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

定義

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

public:
 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

參數

buffer
Byte[]

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

index
Int32

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

count
Int32

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

傳回

Int32

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

例外狀況

buffer 值為 null

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

-或-

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

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

XmlReader 實作不支援這個方法。

項目包含混合內容。

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

範例

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

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.
        reader.ReadToFollowing("image");
        // 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);
        }
        outputFile.Close();
  }
}
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.
            reader.ReadToFollowing("image")
            ' 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
            outputFile.Close()
        
    End Using

End Sub

備註

這個方法會讀取專案內容、使用 BinHex 編碼來解碼,並將解碼的二進位位元組傳回 (,例如,內嵌 BinHex 編碼 GIF 影像) 緩衝區。

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

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

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

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

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

適用於

另請參閱