Hello..

I am creating Contact Book Application.. and I am now having a problem while writing this information to an XML File...

I get "Root Element is Missing" that crashes the application while using XmlDocument.Load to load a Stream

Simply it should save all the changes when closing the form...

Here is a portion of the code where the problem happens

private void Form1_Load(object sender, EventArgs e)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if(!Directory.Exists(path + "\\Address Book - Contacts"))
                Directory.CreateDirectory(path + "\\Address Book - Contacts");
            if (!File.Exists(path + "\\Address Book - Contacts\\settings.xml"))
            {
                XmlTextWriter xW = new XmlTextWriter(path + "\\Address Book - Contacts\\settings.xml", Encoding.UTF8);
                xW.WriteStartElement("people");
                xW.WriteEndElement();
                xW.Close();
            }
        }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            XmlDocument xDoc = new XmlDocument();
            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            xDoc.Load(path + "\\Address Book - Contacts\\settings.xml"); // Problem Happens Here <<<<<<<<<<<<<<<<<<

            XmlNode xNode = xDoc.SelectSingleNode("People"); // Prevent dumping... and write the existing new
            xNode.RemoveAll();

            foreach (Person p in people)
            {
                XmlNode xTop = xDoc.CreateElement("Person");

                XmlNode xName = xDoc.CreateElement("Name");
                XmlNode xEmail = xDoc.CreateElement("E-mail");
                XmlNode xPhone = xDoc.CreateElement("Phone Number");
                XmlNode xAddress = xDoc.CreateElement("Street Address");
                XmlNode xBirthday = xDoc.CreateElement("Birthday");
                XmlNode xNotes = xDoc.CreateElement("Additional Notes");

                xName.InnerText = p.Name;
                xEmail.InnerText = p.Email;
                xPhone.InnerText = p.Phone;
                xAddress.InnerText = p.StreetAddress;
                xBirthday.InnerText = p.Birthday.ToFileTime().ToString(); //Convert DateTime to FileTime then to String 
                xNotes.InnerText = p.Notes;
                                                
                xTop.AppendChild(xName);
                xTop.AppendChild(xEmail);
                xTop.AppendChild(xPhone);
                xTop.AppendChild(xAddress);
                xTop.AppendChild(xBirthday);
                xTop.AppendChild(xNotes);         //Append All the Information to xTop Person so each property contains information for 1 person node.

                xDoc.DocumentElement.AppendChild(xTop); // Append xTop to the Actual Document

                // (xName, xEmail, .... ) Name, Email,.... -> (xTop) Person -> (xDoc) XML File 

            }
            xDoc.Save(path + "\\Address Book - Contacts\\settings.xml");
        }

Could anyone help me with this quickly ??!

Thanks a lot in advance :)

Is your settings.xml malformed?
Have you looked at it (with a text editor) after you created it?

You have to surround your xml in a tag, for example:

<AddressBook>
         <Contact>
             <Name>
             Bob
             </Name> 
         </Contact>
</AddressBook>

If you have already done that them I am not too sure what to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.