4

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.Save method to save to a pre-defined location, but how can I allow the user to save to any location like when choosing 'SaveAs'?

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\OriginalFile.xml");
doc.Save("File.xml");
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
KeyboardFriendly
  • 1,798
  • 5
  • 32
  • 42
  • 1
    Possible duplicate of [this question](http://stackoverflow.com/questions/5622854/how-do-i-show-a-save-as-dialog-in-wpf) – jszigeti Feb 16 '13 at 03:26

2 Answers2

2

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

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Xml (*.xml)|*.xml";
if (saveFileDialog.ShowDialog().Value)
{
    doc.Save(saveFileDialog.FileName);
}
Howard
  • 3,638
  • 4
  • 32
  • 39
2

Use SaveFileDialog. Sample from the article:

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;
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179