WCF技术内幕 第5章 (3)
使用XmlDictionary
using (MemoryStream stream = new MemoryStream()) { using (XmlDictionaryWriter xtw = XmlDictionaryWriter.CreateBinaryWriter(stream, null, null)) { xtw.WriteElementString("Person", "urn:Andersoft", "Mike"); xtw.Flush(); Console.WriteLine("XmlDictionaryWriter wrote {0} bytes", stream.Position); //29 stream.Position = 0; Byte[] bytes = stream.ToArray(); File.WriteAllText(@"c:\file.txt", BitConverter.ToString(bytes)); //40-06-50-65-72-73-6F-6E-08-0D-75-72-6E-3A-41-6E-64-65-72-73-6F-66-74-99-04-4D-69-6B-65 File.WriteAllText(@"c:\file1.txt", new StreamReader(stream).ReadToEnd()); //@Person //urn:Andersoft?Mike } }没有使用XMLDictionary压缩过的二进制编码要29个byte,比用XMLDictionary压缩过的二进制编码byte数多了19个。。。
如果使用XMLDictionary,它会直接记录element和attribute的值,但是会把stream中的element name和XML namespaces替换成单个字节。