开发者

how can i make xml for myXMLHttpRequest.responseXML with XmlTextWriter in .ashx

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();
            }
        }
    }


Dont have access to visual studio right now so doing this from top of the head. Should five you a starting point.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/xml";

    using (XmlWriter writer = XmlWriter.Create(context.Response.OutputStream))
    {
        writer.WriteStartDocument();
        // do xmlwriter stuff here.
        writer.WriteEndDocument();
    }
}

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:

int fieldIndex = reader.GetOrdinal("address");
reader.GetString(fieldIndex);

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜