閱讀英文

共用方式為


XmlWriter.WriteBase64(Byte[], Int32, Int32) 方法

定義

在衍生類別中覆寫時,以 Base64 格式編碼指定的二進位位元組,並寫出產生的文字。

public abstract void WriteBase64(byte[] buffer, int index, int count);

參數

buffer
Byte[]

要編碼的位元組陣列。

index
Int32

緩衝區中的位置指示要寫入的位元組開頭。

count
Int32

要寫入的位元組數。

例外狀況

buffernull

indexcount 小於零。

-或-

緩衝區長度減去 index 會小於 count

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

範例

下列範例會 WriteBase64 使用 方法來寫入 Base64 資料。 資料 Base64 會內嵌在 元素 <image> 內。


public static void Base64EncodeImageFile() {

  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 Base64 data...");

       do {
          readBytes = br.Read(buffer, 0, bufferSize);
          writer.WriteBase64(buffer, 0, readBytes);
       } while (bufferSize <= readBytes);
       br.Close();
        
    writer.WriteEndElement();// </image>
    writer.WriteEndDocument();
  }
}

備註

例如,位元組緩衝區可能包含 GIF 影像的二進位內容。 這顯然不是有效的 XML。 編碼 Base64 是設計來代表由 65 個 US-ASCII 字元組成的文字格式的任意位元組序列 ([A-Za-z0-9+/=]) ,其中每個字元都會編碼 6 位的二進位資料。 如需詳細資訊,請參閱要求批註網站中的 RFC) 1521 (批註要求

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

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0