Fuller
Fuller

Reputation: 301

Writing to user defined XML file

How can I prompt the user for the location to which to save an XML file, and then save the file to that location?

Following a tutorial, I have the written code below that writes XML to a file with a hardcoded file name and location. I would like instead to have a dialog box appear and let the user select a location and enter the file name themselves. How can I do that?

Here is my code:

    private void button6_Click(object sender, EventArgs e)
    {
        string path = Directory.GetCurrentDirectory();

        if (!Directory.Exists(path + "\\TaskList")) // if the file directory doesnt exist..
            Directory.CreateDirectory(path + "\\TaskList"); // create the file directory

        if (!File.Exists(path + "\\TaskList\\settings.xml")) // if the XML file doesnt exist..
        {
            XmlTextWriter xW = new XmlTextWriter(path + "\\TaskList\\settings.xml", Encoding.UTF8); // create the xml file
            xW.WriteStartElement("TaskList");
            xW.WriteEndElement();
            xW.Close();
        }

        // create XML document to write to
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(path + "\\TaskList\\settings.xml");

        // create node for every property inside the taskProperties class
        foreach (taskProperties newTask in task)
        {
            XmlNode nodeTop = xDoc.CreateElement("Task");
            XmlNode nodeTitle = xDoc.CreateElement("Title");
            XmlNode nodeDescription = xDoc.CreateElement("Description");
            XmlNode nodePriority = xDoc.CreateElement("Priority");
            XmlNode nodeCompletionDate = xDoc.CreateElement("CompletionDate");
            XmlNode nodeTaskComplete = xDoc.CreateElement("TaskComplete");

            nodeTitle.InnerText = newTask.title;
            nodeDescription.InnerText = newTask.description;
            nodePriority.InnerText = newTask.priority;
            nodeCompletionDate.InnerText = newTask.completionDate.ToFileTime().ToString(); // convert to file time (numbers) then to a string
            nodeTaskComplete.InnerText = newTask.taskComplete;

            // add these nodes to the 'nodeTop' node
            nodeTop.AppendChild(nodeTitle);
            nodeTop.AppendChild(nodeDescription);
            nodeTop.AppendChild(nodePriority);
            nodeTop.AppendChild(nodeCompletionDate);
            nodeTop.AppendChild(nodeTaskComplete);
            // add the nodeTop to the document
            xDoc.DocumentElement.AppendChild(nodeTop);
        }
        // save the document
        xDoc.Save(path + "\\TaskList\\settings.xml");
    }

Upvotes: 1

Views: 223

Answers (1)

Saagar Elias Jacky
Saagar Elias Jacky

Reputation: 2688

Use this to open the File Save Dialog box and get the Location which the user selects.

SaveFileDialog fdgSave= new SaveFileDialog(); 
fdgSave.InitialDirectory = Convert.ToString(Directory.GetCurrentDirectory()); 
fdgSave.Filter = "XML (*.XML)|*.xml|All Files (*.*)|*.*" ; 
fdgSave.FilterIndex = 1; 

if(fdgSave.ShowDialog() == DialogResult.OK) 
{ 
    Console.WriteLine(fdgSave.FileName);//Do what you want here
} 

Upvotes: 3

Related Questions