2012-03-27 110 views
0

我已經使用xmltextwriter創建了xml文件並保存在開發D:驅動器上。現在我想允許用戶使用對話框將文件保存在所需位置。使用保存對話框保存已創建的XML文件

感謝

+0

是代碼 xmlFileName = 「EFIX.036003.CMF.FIX。」 + SDATE + 「CMF003.xml。」; \t \t \t \t \t \t \t \t \t \t XmlTextWriter的瓦特=新的XmlTextWriter(@ 「d:\」 + xmlFileName,Encoding.UTF8); \t \t \t w.Formatting = Formatting.Indented; – SeeSharp 2012-03-27 07:48:39

回答

0

不指定環境,這裏的WinForms代碼片段:

static class Example 
{ 
    public static XmlTextWriter GetWriterForFolder(string fileName, Encoding encoding) 
    { 
     FolderBrowserDialog dlg = new FolderBrowserDialog(); 
     if (dlg.ShowDialog() != DialogResult.OK) 
      return null; 

     XmlTextWriter writer = new XmlTextWriter(Path.Combine(dlg.SelectedPath, fileName), encoding); 
     writer.Formatting = Formatting.Indented; 

     return writer; 
    } 

    public static XmlTextWriter GetWriterForFile(Encoding encoding) 
    { 
     SaveFileDialog dlg = new SaveFileDialog(); 
     dlg.Filter = "XML Files (*.xml)|*.xml"; 

     if (dlg.ShowDialog() != DialogResult.OK) 
      return null; 

     XmlTextWriter writer = new XmlTextWriter(dlg.FileName, encoding); 
     writer.Formatting = Formatting.Indented; 

     return writer; 
    } 
} 

GetWriterForFolder功能讓用戶選擇,其中文件將被保存的文件夾,你必須提供一個文件名作爲參數。就像這樣:

string fileName = "EFIX.036003.CMF.FIX." + sDate + ".CMF003.xml"; 
XmlTextWriter writer = Example.GetWriterForFolder(fileName, Encoding.UTF8); 

GetWriterForFile功能讓用戶選擇一個文件夾,使用的文件名。像這樣:

XmlTextWriter writer = Example.GetGetWriterForFile(Encoding.UTF8); 
下面
+0

感謝@ adriano爲您的回覆 你可以告訴我在web應用程序按鈕單擊我怎樣才能顯示保存對話框到用戶 下面是我的代碼按鈕點擊 xmlFileName =「EFIX.036003.CMF.FIX。」+ SDATE + 「CMF003.xml」; \t \t \t \t \t \t \t \t \t \t XmlTextWriter的瓦特=新的XmlTextWriter(@ 「d:\」 + xmlFileName,Encoding.UTF8); \t \t \t w.Formatting = Formatting.Indented; \t \t \t \t \t \t \t \t \t w.WriteStartDocument(); \t \t \t w.WriteStartElement(「Document」); \t \t \t w.WriteAttributeString(「xmlns:xsi」,「http://www.w3.org/2001/XMLSchemainstance」); w.WriteEndElement(); \t \t \t w.WriteEndDocument(); \t \t \t \t \t \t \t w.Close(); – SeeSharp 2012-03-27 08:19:21

+0

在Web應用程序中?用ASP.NET標記你的問題!它真的**不同! Plain ASP.NET? MVC?服務器應該提供一個URL來創建該文件,用戶可以單擊一個鏈接(或一個LinkBut​​ton)來下載該文件並將其保存在本地。 – 2012-03-27 08:58:22

+0

大家好,我已經使用httpresponse來解決我的問題。創建XML後我已經使用httpresponse與內容類型文本/ xml 感謝所有 – SeeSharp 2012-03-29 09:22:13