Using XML in the .NET Framework " Chapter 4 147 Figure 4.17 Continued saveFileDialog1.OverwritePrompt = true; DialogResult result = saveFileDialog1.ShowDialog(); if(result.ToString().ToLower() == "ok") { this.myDocument = new XmlDocument(); XmlProcessingInstruction myProc = myDocument.CreateProcessingInstruction( "xml", "version='1.0'"); XmlElement myElement = myDocument.CreateElement("addressBook"); myDocument.AppendChild(myProc); myDocument.AppendChild(myElement); myDocument.Save(saveFileDialog1.FileName); this.loadAddressBook(saveFileDialog1.FileName); } } After the user has successfully selected and named a file to create, a new XmlDocument is created (this is a class-level variable). A processing instruction is added (the <?xml version=1.0?> instruction), and the root addressBook element is added. Finally, the myDocument variable is saved and the loadAddressBook() method is called. A very similar process happens when a user wants to open an existing address book file from the file system. See Figure 4.18. Figure 4.18 Opening an Existing XML Address Book File private void mnuFileOpen_Click(object sender, System.EventArgs e) { openFileDialog1.CheckFileExists = true; openFileDialog1.CheckPathExists = true; openFileDialog1.Filter = "XML Address book files (*.xmlab)|*.xmlab"; openFileDialog1.Title = "Open Address Book file"; www.syngress.com Continued 155_xml_net_pd_C04.qk 3/6/02 1:57 PM Page 147