1

XML を生成しようとすると、次の例外が発生します。

XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("userInfo");

それは私に例外を与えます:

WriteStartDocument needs to be the first call.

しかし、ご覧のとおり、最初に WriteStartDocument() を呼び出しました。

何か案は?

4

3 に答える 3

2

ただし、応答ストリームには既に他のものがあります (HTTP ヘッダーなど)。

XML を StringWriter に書き込んでから、その文字列を Response に書き込む方がよいでしょう。

于 2009-04-07T15:46:02.077 に答える
2

これを使用してみてください:

XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument(false);
xmlWriter.WriteStartElement("userInfo");
于 2011-10-10T13:24:18.803 に答える
1

Page ディレクティブのみが残るように、コンテンツの aspx ファイルをクリアすることを忘れないでください。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

また、Response.OutputStream の代わりに Response.Output を使用します。

XmlTextWriter xmlWriter = new XmlTextWriter(Response.Output); 
xmlWriter.WriteStartDocument(); 
xmlWriter.WriteStartElement("userInfo");
xmlWriter.WriteEndElement();
于 2009-04-07T15:49:18.437 に答える