System.Xml.XmlTextWriter.WriteBase64 方法
方法描述
将指定的二进制字节编码为 Base64 并写出结果文本。
语法定义(C# System.Xml.XmlTextWriter.WriteBase64 方法 的用法)
public override void WriteBase64( byte[] buffer, int index, int count )
参数/返回值
参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
---|---|---|
buffer | System-Byte[] | 要进行编码的字节数组。 |
index | System-Int32 | 缓冲区中的位置,指示要写入的字节的起始位置。 |
count | System-Int32 | 要写入的字节数。 |
返回值 | void |
提示和注释
注意
在 .NET Framework 2.0 版 版本中,推荐的做法是使用 XmlWriter.Create 方法和 XmlWriterSettings 类创建 XmlWriter 实例。 这使您可以充分利用此版本中引入的所有新功能。 有关更多信息,请参见 创建 XML 编写器。
System.Xml.XmlTextWriter.WriteBase64 方法例子
使用 ReadBase64 方法对临时 XML 文件进行解码并将其与原文件进行比较。
using System; using System.IO; using System.Xml; using System.Text; class TestBase64 { private const int bufferSize=4096; public static void Main(String[] args) { TestBase64 testBase64=new TestBase64(); //Check that the usage string is correct. if (args.Length < 2 ) { testBase64.Usage(); return; } FileStream fileOld = new FileStream(args[0], FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read); testBase64.EncodeXmlFile("temp.xml", fileOld); FileStream fileNew = new FileStream(args[1], FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); testBase64.DecodeOrignalObject("temp.xml", fileNew); //Compare the two files. if (testBase64.CompareResult( fileOld, fileNew)) { Console.WriteLine("The recreated binary file {0} is the same as {1}", args[1], args[0] ); } else { Console.WriteLine("The recreated binary file {0} is not the same as {1}", args[1], args[0] ); } fileOld.Flush(); fileNew.Flush(); fileOld.Close(); fileNew.Close(); } //Use the WriteBase64 method to create an XML document. The object //passed by the user is encoded and included in the document. public void EncodeXmlFile(String xmlFileName, FileStream fileOld) { byte[] buffer = new byte[bufferSize]; int readByte=0; XmlTextWriter xw = new XmlTextWriter(xmlFileName, Encoding.UTF8); xw.WriteStartDocument(); xw.WriteStartElement("root"); // Create a Char writer. BinaryReader br = new BinaryReader(fileOld); // Set the file pointer to the end. try { do { readByte=br.Read(buffer, 0, bufferSize); xw.WriteBase64(buffer, 0, readByte); } while (bufferSize <= readByte ); } catch (Exception ex) { EndOfStreamException ex1= new EndOfStreamException(); if (ex1.Equals(ex)) { Console.WriteLine("We are at end of file"); } else { Console.WriteLine(ex); } } xw.WriteEndElement(); xw.WriteEndDocument(); xw.Flush(); xw.Close(); } //Use the ReadBase64 method to decode the new XML document //and generate the original object. public void DecodeOrignalObject(String xmlFileName, FileStream fileNew) { byte[] buffer = new byte[bufferSize]; int readByte=0; //Create a file to write the bmp back. BinaryWriter bw = new BinaryWriter(fileNew); XmlTextReader tr = new XmlTextReader(xmlFileName); tr.MoveToContent(); Console.WriteLine(tr.Name); do { readByte=tr.ReadBase64(buffer, 0, bufferSize); bw.Write(buffer, 0, readByte); } while(readByte>=bufferSize); bw.Flush(); } //Compare the two files. public bool CompareResult(FileStream fileOld, FileStream fileNew) { int readByteOld=0; int readByteNew=0; int count, readByte=0; byte[] bufferOld = new byte[bufferSize]; byte[] bufferNew = new byte[bufferSize]; BinaryReader binaryReaderOld = new BinaryReader(fileOld); BinaryReader binaryReaderNew = new BinaryReader(fileNew); binaryReaderOld.BaseStream.Seek(0, SeekOrigin.Begin); binaryReaderNew.BaseStream.Seek(0, SeekOrigin.Begin); do { readByteOld=binaryReaderOld.Read(bufferOld, 0, bufferSize); readByteNew=binaryReaderNew.Read(bufferNew, 0, bufferSize); if (readByteOld!=readByteNew) { return false; } for (count=0; count
异常
异常 | 异常描述 |
---|---|
ArgumentNullException | buffer 为 null。 |
ArgumentException | 缓冲区长度减去 index 小于 count。 |
ArgumentOutOfRangeException | index 或 count 小于零。 |
InvalidOperationException | WriteState 为 Closed。 |
版本信息
.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 系统要求。