private void openToolStripMenuItem_Click(object sender, EventArgs e) { // Show an open file dialog OpenFileDialog OpenDlg = new OpenFileDialog(); OpenDlg.RestoreDirectory = true; OpenDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (OpenDlg.ShowDialog() == DialogResult.OK) { // Filename to open string FileToOpen = OpenDlg.FileName; // Read the xml contents to a string string xmlContents = File.ReadAllText(FileToOpen); // Declare a deserializer. XmlSerializer deserializer = new XmlSerializer(typeof(PitchATentJob)); // Deserialize to list of TentListItems objects. using (TextReader reader = new StringReader(xmlContents)) { // Get the information from the file PitchATentJob Job = (PitchATentJob)deserializer.Deserialize(reader); // Write UI Elements WriteTentDGV(Job.TentList, true); WriteAccDGV(Job.AccList, true); } } }
private void SaveJob(string filename = @"JobList.xml") { XmlWriterSettings xmlWriterSettings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }; string utf8; using (MemoryStream memoryStream = new MemoryStream()) using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings)) { XmlSerializer serializer = new XmlSerializer(typeof(PitchATentJob)); // Read the tentDGV List <TentListItem> TentList = ReadTentDGV(); // Read the accDGV List <Accessory> AccList = ReadAccDGV(); // Construct an object from both lists PitchATentJob Job = new PitchATentJob(TentList, AccList); if (filename == @"JobList.xml") { filename = @"C:\ProgramData\Charbonneau Vendette Solutions\" + filename; } else { SaveFilePath = filename; } if (TentList != null) { using (StringWriter writer = new Utf8StringWriter()) { serializer.Serialize(writer, Job); utf8 = writer.ToString(); File.WriteAllText(filename, utf8); JobSaved = true; } } } }