我使用xmltextwriter创建xml文件并保存在开发D:驱动器上。现在我想允许用户使用对话框将文件保存在所需的位置。
感谢
答案 0 :(得分: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);