我正在尝试将压缩的xml数据写入内存流。这是我的测试控制台应用程序代码:
var ms = new MemoryStream();
var gstream = new GZipStream(ms, CompressionMode.Compress);
var settings = new XmlWriterSettings();
settings.Indent = true;
var writer = XmlWriter.Create(gstream, settings);
writer.WriteStartElement("x", "root", "123");
writer.WriteStartElement("item");
writer.WriteAttributeString("xmlns", "x", null, "abc");
writer.WriteEndElement();
writer.WriteEndElement();
Console.WriteLine("Data length: " + ms.ToArray().Length);
writer.Close();
gstream.Close();
ms.Close();
Console.ReadLine();
输出是:
Data length: 0
我做错了什么?谢谢。
答案 0 :(得分:0)
您必须关闭编写器才能刷新流。只需将代码更改为:
writer.Close();
gstream.Close();
ms.Close();
Console.WriteLine("Data length: " + ms.ToArray().Length);
它应该有用。