protected void Button1_Click(object sender, EventArgs e)
    {
        Random random=new Random();
        int rnumber=random.Next(1, 100);
        string outPutName = DateTime.Now.ToString("yyyyMMddhhmmss") + rnumber.ToString();
        HttpResponse resp;
        resp = Page.Response;
        resp.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        resp.ContentType = "text/xml";
        resp.AddHeader("Content-Disposition",
"attachment; filename=" + System.Web.HttpUtility.UrlEncode(outPutName, System.Text.Encoding.UTF8) + ".xml");

        this.EnableViewState = false;

        DataTable dt = new DataTable();
        if (!string.IsNullOrEmpty(txtDONos.Text.Trim()))
        {
            DistributeOrderBLL doBll = new DistributeOrderBLL();
            string strDistributeOrderNos = txtDONos.Text.Trim();
            dt = doBll.GetDistributeOrderByDistributeOrderNos

(strDistributeOrderNos);
        }
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);

        StringBuilder xmlData = new StringBuilder();
        xmlData.AppendLine(@"<?xml version='1.0' encoding='UTF-8' ?>");
        xmlData.AppendLine(@"<root>");
        foreach (DataRow dr in dt.Rows)
        {
            xmlData.AppendLine(@"<person");
            xmlData.AppendLine(@" id='" + dr["Name"] + "' ");
            xmlData.AppendLine(@">");

            xmlData.AppendLine(@"<name>" + dr["Name"] + "</name>");
            xmlData.AppendLine(@"<address>" + dr["Street"] + "</address>");
            xmlData.AppendLine(@"<city>" + dr["CityName"] + "</city>");
            xmlData.AppendLine(@"</person>");
        }
        xmlData.AppendLine(@"</root>");
        Response.Write(xmlData.ToString());

        Response.End();

    }

 另外一种办法

 public void toMakXmlFile(ArrayList lfname, ArrayList lfile,string xmlName)
        {

            System.IO.StringWriter sw = new System.IO.StringWriter();
            xmlW = new XmlTextWriter(sw);
            xmlW.Formatting = Formatting.Indented;
            xmlW.WriteStartElement("Lables");
            for (int i = 0; i < lfile.Count; i++)
            {
                xmlW.WriteStartElement("LableItem");


                xmlW.WriteStartElement("lfile");
                xmlW.WriteAttributeString("lfname", (string)lfname[i]);
                xmlW.WriteString((string)lfile[i]);
                xmlW.WriteEndElement();

                xmlW.WriteEndElement();
            }
            xmlW.WriteEndElement();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); // 中文
            HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + xmlName + ".xml\"");
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();

        }

 读取的一种办法

 //读取xml文件
                            XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(e.FullPath);
                            XmlNode xn = xmlDoc.SelectSingleNode("root");
                            XmlNodeList xnl = xn.ChildNodes;
                            foreach (XmlNode xnf in xnl)
                            {
                                XmlElement xe = (XmlElement)xnf;
                                XmlNodeList xnf1 = xe.ChildNodes;
                                string name = xnf1.Item(0).InnerText;
                                string address = xnf1.Item(1).InnerText;
                                string city = xnf1.Item(2).InnerText;
                                string state = xnf1.Item(3).InnerText;
                                string postcode = xnf1.Item(4).InnerText;
                                string country = xnf1.Item(5).InnerText;
                                string phone1 = xnf1.Item(6).InnerText;

                             }

posted on 2009-06-22 14:48  冷火  阅读(988)  评论(0编辑  收藏  举报