我如何使用 .ashx 中的 XmlTextWriter 为 myXMLHttpRequest.responseXML 制作 xml

发布于 2024-11-07 07:30:15 字数 1848 浏览 0 评论 0原文

我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2024-11-14 07:30:15

现在无法访问 Visual Studio,因此需要从头开始执行此操作。应该以五个你为起点。

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

更新:

我确实想把OutputStream放在那里。我更新了示例以反映这一点。

已经简要地查看过您的代码。我可以看到您没有将 xmlwriter 的结果输出到 HttpResponse。 (我的示例通过将其绑定到 Context.Response.OutputStream 来实现)这应该是它不起作用的原因。虽然我不明白为什么会发生 500 错误。也许您应该在 Visual Studio 中运行以查看是否发生任何导致此问题的错误。

我还注意到您使用了reader[0]。事件艰难,这在技术上可能是正确的,我建议您使用更安全的方法,如下所示:

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

这将确保您始终从查询中获得所需的字段。即使将来您决定更改数据库架构以添加更多列。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文