wpf 将编辑的 XML 文档保存到任何位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14906555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:33:23  来源:igfitidea点击:

Save edited XML Document to any location?

c#wpfxmlc#-4.0

提问by KeyboardFriendly

In the below C# WPF code snippet, I want to load an XML document, edit the document, and save the output to a user designated location. I can use the XmlDocument.Savemethod to save to a pre-defined location, but how can I allow the user to save to any location like when choosing 'SaveAs'?

在下面的 C# WPF 代码片段中,我想加载一个 XML 文档,编辑该文档,并将输出保存到用户指定的位置。我可以使用该XmlDocument.Save方法保存到预定义的位置,但是如何允许用户保存到任何位置,例如选择“另存为”时?

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\OriginalFile.xml");
doc.Save("File.xml");

采纳答案by Howard

see the code below; be aware that the UAC if the user select some system folder.

看下面的代码;请注意,如果用户选择了某个系统文件夹,则 UAC 将被禁用。

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Xml (*.xml)|*.xml";
if (saveFileDialog.ShowDialog().Value)
{
    doc.Save(saveFileDialog.FileName);
}

回答by Alexei Levenkov

Use SaveFileDialog. Sample from the article:

使用SaveFileDialog。文章示例:

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; 
dlg.DefaultExt = ".xml";
dlg.Filter = "Xml documents (.xml)|*.xml"; // Filter files by extension 

Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
    // Save document 
    string filename = dlg.FileName;
}