我在我的.ashx頁面中嘗試了它,但是在myXMLHttpRequest.status中出現錯誤500,我無法理解問題出在哪裏。生成簡單xml的簡單代碼將非常好。像:我怎樣才能使用xmlTextWriter在.ashx中爲myXMLHttpRequest.responseXML生成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();
}
}
}
您能與我們分享您嘗試的解決方案嗎? – 2011-05-15 10:40:09