asp.net匯出xml檔案

來源:互聯網
上載者:User

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;

                             }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.