KeyboardFriendly
KeyboardFriendly

Reputation: 1798

Save edited XML Document to any location?

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");

Upvotes: 4

Views: 6670

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

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;
}

Upvotes: 2

Howard
Howard

Reputation: 3848

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);
}

Upvotes: 2

Related Questions