-3

Im trying to save a file from a DataGrid using a button for it but i dont know how to make it saving that and the user can choose where to save. Also having some problems on the code.

    private void Button_Click_4(object sender, RoutedEventArgs e)
    {
        var path = @"C:\\Users\\Tiago\\Documents\\Teste\\Save.xml";

            if (serializableObject == null) { return; }

            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
                using (MemoryStream stream = new MemoryStream())
                {
                    serializer.Serialize(stream, serializableObject);
                    stream.Position = 0;
                    xmlDocument.Load(stream);
                    xmlDocument.Save(path);
                    stream.Close();
                }
            }
            catch (Exception ex)
            {

            }


    }

This is what i got for now.

Tiago
  • 1
  • 5

4 Answers4

0

If you want to allow user to choose the location and name of file, there is SaveFileDialog option available for you.

Here are the some of the links that can help you to accomplish this:
MSDN Link_1
MSDN Link_2
Link_3

Saadi
  • 2,211
  • 4
  • 21
  • 50
0

using (FileStream fs = new FileStream(path, FileMode.Create) missing a )

Cologler
  • 724
  • 6
  • 18
0

Firstly, you have a missing } curly brace at the end of your code. This might clean up a few issues.

Secondly, make sure your serializableObject is accessible by the Button_Click_4 event. Set the serializableObject as a field or property to access it.

To choose where to save Use a FileSaveDialog to give the user the choice where to save.

Create one :

var fileSaveDialog = new FileSaveDialog();

and then show it:

fileSaveDialog.ShowDialog();

You can then use the filename that the FileSaveDialog object gives when "Save" is clicked.

This is a string - set your path variable to it.

Like this:

var path = fileSaveDialog.FileName;

See http://www.wpf-tutorial.com/dialogs/the-savefiledialog/ for a good tutorial on using the FileSaveDialog.

Example: (use this inside your Button_Click_4 event handler)

var saveFileDialog = new SaveFileDialog(); // Create it

System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog(); // Show the dialog and set a result (bool) to whether the user clicks "Save" or exits out of the dialog

if (result == DialogResult.OK) // If the user clicked "Save"
{
    var path = saveFileDialog.FileName; // Set the path to save to

    // Use the path variable to save the file to disk using your own code
}

Hope this helps!

EDIT:
Don't forget to use using Microsoft.Win32; at the top of your code.

ANOTHER EDIT:
I've updated the code to assign the result as a System.Windows.Forms.DialogResult to compare result == DialogResult.OK.
See: DialogResult.OK on SaveFileDialog not work

Community
  • 1
  • 1
Geoff James
  • 3,122
  • 1
  • 17
  • 36
  • I don't understand what you are saying/asking..? – Geoff James May 09 '16 at 10:33
  • when i save and close the program, the file where i saved it isnt there – Tiago May 09 '16 at 10:35
  • This is because your `FileStream fs` object is locking the file. You don't need the `using (FileStream fs = new ...)` statement wrapped around the `try {} catch {}` block, as the `XmlDocument.Save` method is saving it to disk. It can't do this because your `fs` is currently using the file. – Geoff James May 09 '16 at 10:40
  • Step through your debugger (put a breakpoint at where you show your `saveFileDialog`). See what the `result` is, and see if the `savedialog.FileName` has the right value in and it is being assigned to your `path` BEFORE you save the file with the rest of your code :) – Geoff James May 09 '16 at 10:53
  • the result is null – Tiago May 09 '16 at 10:56
  • That's why it's not saving, then. Make sure that "Save" is being clicked, also. Have a look here: http://www.wpf-tutorial.com/dialogs/the-savefiledialog/ Other than that, I can't debug your code for you – Geoff James May 09 '16 at 10:57
  • Instead of `var result = saveFileDialog.ShowDialog();`, try using `System.Windows.Forms.DialogResult result = saveFileDialog.ShowDialog();` instead. Then in your check, look for `if (result == DialogResult.OK) Let me know if this works: I'll update my answer if so – Geoff James May 09 '16 at 11:00
  • I know you're not, but your SaveFileDialog is a WinForms component. By initializing your `result` variable using the FQ namespace you can ensure that your result is not lost in translation; and you can compare `DialogResult.OK` (which is also WinForms/Win32) enumeration against it – Geoff James May 09 '16 at 11:07
  • OK - here's a reference that gives you an example in the same way I've mentioned: http://stackoverflow.com/questions/23539219/dialogresult-ok-on-savefiledialog-not-work – Geoff James May 09 '16 at 11:15
  • Do i need to creat a class for form? – Tiago May 09 '16 at 11:23
  • I don't get what you mean? Are you getting a red squiggly error underneath `System.Windows.Forms.DialogResult`? If so; you might need to include the reference to `System.Windows.Forms` in your project – Geoff James May 09 '16 at 11:26
  • Doesn't let you what? Can you be more specific, please – Geoff James May 09 '16 at 11:28
  • The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) this error after i put the using System.Windows.Forms; – Tiago May 09 '16 at 11:32
  • OK, this is falling outside the scope of your original question, now - start a message in chat if you need more help – Geoff James May 09 '16 at 11:33
0

To choose where to save, you can use System.Windows.Forms.FolderBrowserDialog class. But this is a WinForm library, so you need to add System.Windows.Forms to your project references.

var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
Jai
  • 8,165
  • 2
  • 21
  • 52