我如何使用 .ashx 中的 XmlTextWriter 为 myXMLHttpRequest.responseXML 制作 xml
我在 .ashx 页面中尝试,但在 myXMLHttpRequest.status 中收到错误 500,我无法理解问题出在哪里。生成简单 xml 的简单代码会非常好。就像:
<properties>
<property>
<address>812 Gwyn Ave</address>
</property>
<property>
<address>3308 James Ave S</address>
</property>
</properties>
@这是我的解决方案(sql代码工作正常):
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = "text/xml";
XmlTextWriter writer = new XmlTextWriter();
string user_id = context.Request.Params["user_id"];
string connectionString = ("Data Source=.;Initial Catalog=user_city;Integrated Security=True");
string queryString = "select * from city_buildings where user_id=" + user_id + ";";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
writer.WriteStartDocument();
try
{
writer.WriteStartElement("buildings");
while (reader.Read())
{
writer.WriteStartElement("building");
writer.WriteElementString("user_id",Convert.ToString( reader[0]));
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
finally
{
reader.Close();
}
}
}
i try it in my .ashx page but i got error 500 in myXMLHttpRequest.status and i can't understand where is the problem. a simple code for generate a simple xml would be very good. like:
<properties>
<property>
<address>812 Gwyn Ave</address>
</property>
<property>
<address>3308 James Ave S</address>
</property>
</properties>
@ here is my solution (sql code works fine):
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = "text/xml";
XmlTextWriter writer = new XmlTextWriter();
string user_id = context.Request.Params["user_id"];
string connectionString = ("Data Source=.;Initial Catalog=user_city;Integrated Security=True");
string queryString = "select * from city_buildings where user_id=" + user_id + ";";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
writer.WriteStartDocument();
try
{
writer.WriteStartElement("buildings");
while (reader.Read())
{
writer.WriteStartElement("building");
writer.WriteElementString("user_id",Convert.ToString( reader[0]));
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
finally
{
reader.Close();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在无法访问 Visual Studio,因此需要从头开始执行此操作。应该以五个你为起点。
更新:
我确实想把OutputStream放在那里。我更新了示例以反映这一点。
已经简要地查看过您的代码。我可以看到您没有将 xmlwriter 的结果输出到 HttpResponse。 (我的示例通过将其绑定到 Context.Response.OutputStream 来实现)这应该是它不起作用的原因。虽然我不明白为什么会发生 500 错误。也许您应该在 Visual Studio 中运行以查看是否发生任何导致此问题的错误。
我还注意到您使用了
reader[0]
。事件艰难,这在技术上可能是正确的,我建议您使用更安全的方法,如下所示:这将确保您始终从查询中获得所需的字段。即使将来您决定更改数据库架构以添加更多列。
Dont have access to visual studio right now so doing this from top of the head. Should five you a starting point.
Update:
I indeed did mean to put OutputStream there. I updated the example to reflect this.
Have been looking at your code briefly. I can see you do not output the result of the xmlwriter to the HttpResponse. (what my example does by binding it to
Context.Response.OutputStream
) That should be a reason why it won't work. Altough why the 500 error occurs i don't see. Maybe you should run in visual studio to see if any errors occur that will cause this.Also i noticed your use of
reader[0]
. Event tough this might technicly be correct i will suggest you use a safer method like the following:This will ensure you will always get the desired field from the query. Even if in the future you decide to change the database schema to add some more columns.