这是代码
public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string uid = context.Request.QueryString["uid"];//用户编号
            string ret = "";
            if (uid != null && uid != "")
            {
                DataTable dt = AskDA.GetPhoneAskLog(uid);
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        string subject = row["subject"].ToString();
                        string nums = row["num"].ToString();
                        ret = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><p subject=\"" + subject + "\" num=\"" + nums + "\"/></root>";                        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                        context.Response.Write(ret);
                    }
                }
            }
        }
调用的方法啥的都没错  就是老提示文档只能有一个顶层元素。....  怎么回事啊 编码也看了 搞不清

解决方案 »

  1.   

    ret 这个是不是应该写到循环外呢,你这样不是循环添加多个root吗
      

  2.   

    不好意思 发错了 是这个文件
    string ret = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>";
                if (uid != null && uid != "")
                {
                    if (subj != null && subj != "")
                    {
                        DataTable dt = AskDA.GetIMAskLogDetail(uid,subj);
                        if (dt.Rows.Count > 0)
                        {
                            foreach (DataRow row in dt.Rows)
                            {
                                string seqid = row["seq_id"].ToString();
                                string gid = row["gid"].ToString();
                                string sname = row["send_user_name"].ToString();
                                string rname = row["recv_user_name"].ToString();
                                string txt = row["txt"].ToString();
                                string subject = row["subject"].ToString();
                                string showflag = row["show_flag"].ToString();
                                string sid = row["send_user_id"].ToString();
                                string rid = row["recv_user_id"].ToString();
                                string cdate = row["create_date"].ToString();                            ret += "<p seq_id=\"" + seqid + "\" gid=\"" + gid + "\" send_user_name =\"" + sname + "\"" +
                                           " recv_user_name =\"" + rname + "\" txt=\"" + txt + "\" subject=\"" + subject + "\" show_flag =\"" + showflag + "\" " +
                                           "send_user_id =\"" + sid + "\" recv_user_id=\"" + rid + "\" create_date=\"" + cdate + "\"/>";
                                ret += "</root>";
                                context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                                context.Response.Write(ret);
                            }
                            
                        }
                    }
      

  3.   

    他老是提示:Message: XML 文档只能有一个顶层元素  错误
      

  4.   

    context.Response.Clear();
    context.Response.ContentType = "text/xml";
    string ret = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>";
    ,...
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.Write(ret);
    context.Response.End();
      

  5.   

    我说把ret 到写入文件之前的值打出来看看。
      

  6.   

    本帖最后由 net_lover 于 2012-03-28 15:55:43 编辑
      

  7.   

    这就是提示 Message: XML 文档只能有一个顶层元素。Line: 1
    Char: 118
    Code: 0
    URI: http://localhost:6310/UCenter/NewTools/PhoneLog.ashx?uid=133不能传图 只能复制下来错误提示了
      

  8.   

    但是把context.Response.Write(ret);和ret += "</root>";结束标记放里面是因为多条数据循环输出  放里面放外面都不行
      

  9.   

    这就是提示  Message: XML 文档只能有一个顶层元素。Line: 1
    Char: 118
    Code: 0
    URI: http://localhost:6310/UCenter/NewTools/PhoneLog.ashx?uid=133不能传图 只能复制下来错误提示了
      

  10.   

    context.Response.Clear();
    context.Response.ContentType = "text/xml";
    string ret = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><root>";
    ,...
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.Write(ret);
    context.Response.End();