使用保存对话框保存创建的XML文件
本文关键字:保存 XML 文件 对话框 创建 | 更新日期: 2023-09-27 18:29:17
我使用xmltextwriter创建了xml文件,并保存在开发D:drive上。现在我想允许用户使用对话框将文件保存在所需的位置。
感谢
您没有指定环境,这里是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);