我们有一个第三方解析的VXML项目,为我们提供了一个电话导航系统。我们要求他们输入id代码来留言,我们公司随后会对其进行审核。
我们目前的工作方式如下:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Stream m = new MemoryStream(); //Create Memory Stream - Used to create XML document in Memory
XmlTextWriter XML_Writer = new XmlTextWriter(m, System.Text.Encoding.UTF8);
XML_Writer.Formatting = Formatting.Indented;
XML_Writer.WriteStartDocument();
/* snip - writing a valid XML document */
XML_Writer.WriteEndDocument();
XML_Writer.Flush();
m.Position = 0;
byte[] b = new byte[m.Length];
m.Read(b, 0, (int)m.Length);
XML_Writer.Close();
HttpContext.Current.Response.Write(System.Text.Encoding.UTF8.GetString(b, 0, b.Length));我只是在维护这个应用程序,我没有写it...but结尾部分对我来说似乎很复杂。
我知道它接受输出流并将编写的XML提供给it...but,为什么它首先读取整个字符串?这不是很低效吗?
有没有更好的方法来写上面的代码?
发布于 2008-09-30 19:53:28
可以,只需直接写入响应Output (IO.StreamWriter)或OutputStream (IO.Stream)即可:
XmlTextWriter XML_Writer = new XmlTextWriter(HttpContext.Current.Response.OutputStream, HttpContext.Current.Response.Encoding);
//...
XML_Writer.Flush();发布于 2008-09-30 19:56:04
在那之后,我可以直接调用XML_Writer.Flush(),对吗?这会将XML刷新到流吗?
发布于 2008-09-30 19:57:30
您可以直接写入响应流:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
XmlWriter XML_Writer = XmlWriter.Create(HttpContext.Current.Response.Output);
要向编写器添加设置,最好使用较新的XmlWriterSettings类。将其作为参数提供给XmlWriter.Create函数。
https://stackoverflow.com/questions/154655
复制相似问题