none
關於XML string如何轉成xml檔案? RRS feed

  • 問題

  • 請問我要怎麼把xmlizedString值丟給下面由網頁轉xml檔案的方法呢...?

     

                

    程式碼區塊

    XmlSerializer xmlSerializer = new XmlSerializer( typeof( InfoDataCollection ) );

                string xmlizedString = null;

                string nameSpace = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";

                MemoryStream memoryStream = new MemoryStream();

                XmlTextWriter xmlTextWriter = new XmlTextWriter( memoryStream, Encoding.UTF8 );

                xmlSerializer.Serialize( xmlTextWriter, infoDataCollection, null );

                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

                xmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()).Replace(nameSpace, string.Empty);

     

     

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "utf-8";

                Response.AppendHeader( "Content-Disposition", "attachment;filename = ExportedFile.xml" );

                Response.ContentEncoding = System.Text.Encoding.GetEncoding( "utf-8" );

                Response.ContentType = "application/xml";
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter( stringWrite );

               
                this.EnableViewState = false;
                Response.Write(stringWrite.ToString());
                Response.End();

     

     

    2007年11月8日 下午 02:13

所有回覆

  • 程式碼區塊

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(InfoDataCollection));
            StringWriter stringWriter = new StringWriter();
            xmlSerializer.Serialize(stringWriter, infoDataCollection);
            string xmlizedString = stringWriter.ToString();

     

            string filename = "ExportedFile.xml";
            Response.ContentType = "application/force-download";
            Response.AppendHeader("contect-transfer Encoding", "Binary");
            Response.AppendHeader("content-disposition", "attachment; filename=" + filename);
            Response.Write(xmlizedString);
            Response.End();

     

     

    2007年11月8日 下午 03:27