System.Xml.XmlWriter.WriteBase64 方法

方法描述

当在派生类中被重写时,将指定的二进制字节编码为 Base64 并写出结果文本。

语法定义(C# System.Xml.XmlWriter.WriteBase64 方法 的用法)

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

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
buffer System-Byte[] 要进行编码的字节数组。
index System-Int32 缓冲区中指示要写入字节的起始位置的位置。
count System-Int32 要写入的字节数。
返回值 void

提示和注释

例如,字节缓冲区可以包含 GIF 图像的二进制内容。 这显然不会是有效的 XML。 Base64 编码方式设计用于表示由 65 个 US-ASCII 字符 ([A-Za-z0-9+/=]) 组成的文本形式的任意字节序列,其中每个字符对 6 位二进制数据进行编码。 有关更多信息,请参见“请求注释”网站 (http://www.rfc-editor.org/) 上的“请求注释 (RFC) 1521”。

System.Xml.XmlWriter.WriteBase64 方法例子

Base64 数据嵌入到 元素中。

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();// 
    writer.WriteEndDocument();
		
  }
}

异常

异常 异常描述
ArgumentNullException buffer 为 null。
ArgumentOutOfRangeException
  • index 或 count 小于零。
  • 缓冲区长度减去 index 小于 count。

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。