如何在asp.net中刷新xml文件?

时间:2014-01-03 16:33:21

标签: asp.net xml response

我想将xml文件返回给客户端,然后执行我的逻辑。 我尝试使用Response.Flush(),但它对我来说无法正常工作。

我的代码:

protected void Page_Load(object sender, EventArgs e)
{
string arg1= this.Request["arg1"];
string arg2= this.Request["arg2"];
string arg3= this.Request["arg3"];

var doc = new XmlDocument();
XmlNode responseNode = doc.CreateElement("response");
doc.AppendChild(responseNode);

var messageNode = responseNode.AppendChild(doc.CreateElement("message"));
messageNode.InnerText = "Your request is being processed";

Response.Clear();
Response.ContentType = "text/xml"; 
Response.ContentEncoding = System.Text.Encoding.UTF8;
doc.Save(Response.Output);
Response.Flush(); // I want to display xml in this moment but it doesn't work

LogicManager manager = new LogicManager();
manager.DoSth(arg1, arg2, arg3);

Response.End();
}

1 个答案:

答案 0 :(得分:0)

  

到Response.End();

通常,在代码中使用Response.End()是个坏主意。

From the source:此方法仅用于与ASP兼容,即与ASP.NET之前基于COM的Web编程技术兼容。如果要跳转到EndRequest事件并向客户端发送响应,通常最好调用CompleteRequest。

鉴于此信息,而不是:

Response.Flush(); // I want to display xml in this moment but it doesn't work

LogicManager manager = new LogicManager();
manager.DoSth(arg1, arg2, arg3);

Response.End();

这样做:

Response.Flush(); // I want to display xml in this moment but it doesn't work
Response.CompleteRequest();

LogicManager manager = new LogicManager();
manager.DoSth(arg1, arg2, arg3);
// here you might want to make sure that no other page handlers are executed