2012-03-27 108 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