c# - Writing to user defined XML file -
i have been following youtube tutorial on creating lists , saving/loading them xml files. however, have been playing around , cant quite seem head around having dialog box appear , let user select location , enter file name themselves. @ moment predefined file name , location on written every time save it. not want.
here code:
private void button6_click(object sender, eventargs e) { string path = directory.getcurrentdirectory(); if (!directory.exists(path + "\\tasklist")) // if file directory doesnt exist.. directory.createdirectory(path + "\\tasklist"); // create file directory if (!file.exists(path + "\\tasklist\\settings.xml")) // if xml file doesnt exist.. { xmltextwriter xw = new xmltextwriter(path + "\\tasklist\\settings.xml", encoding.utf8); // create xml file xw.writestartelement("tasklist"); xw.writeendelement(); xw.close(); } // create xml document write xmldocument xdoc = new xmldocument(); xdoc.load(path + "\\tasklist\\settings.xml"); // create node every property inside 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 file time (numbers) string nodetaskcomplete.innertext = newtask.taskcomplete; // add these nodes 'nodetop' node nodetop.appendchild(nodetitle); nodetop.appendchild(nodedescription); nodetop.appendchild(nodepriority); nodetop.appendchild(nodecompletiondate); nodetop.appendchild(nodetaskcomplete); // add nodetop document xdoc.documentelement.appendchild(nodetop); } // save document xdoc.save(path + "\\tasklist\\settings.xml"); }
any on how can achieve appreciated.
use open file save dialog box , location 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 want here }
Comments
Post a Comment