WriteStartDocument() 抛出 InvalidOperationException
我正在尝试生成 XML,但遇到了此异常:
XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("userInfo");
它给了我一个异常:
WriteStartDocument 需要是第一个调用。
但如您所见,我确实首先调用了 WriteStartDocument()!
有任何想法吗?
I'm trying to generate XML and I encounter this exception:
XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("userInfo");
It gives me an exception:
WriteStartDocument needs to be the first call.
But as you can see, I did call the WriteStartDocument() first!
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
然而,响应流中已经存在其他内容(例如 HTTP 标头)。
最好将 XML 写入 StringWriter,然后将字符串写入 Response。
However there are already other things in the Response stream (e.g. HTTP headers).
Probably better to write XML to a StringWriter and then write the string to Response.
尝试使用这个:
Try using this:
不要忘记清除 aspx 文件的内容,以便只留下 Page 指令,即:
还使用 Response.Output 而不是 Response.OutputStream:
Don't forget to clear your aspx file of content so that only the Page directive is left, i.e.:
Also use Response.Output instead of Response.OutputStream: