Hello!

private void btnRecord_Click(object sender, EventArgs e)
        {
            //try
            //{
                Random num = new Random(2);
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement contact = xmlDoc.CreateElement("contact");
                XmlElement name = xmlDoc.CreateElement("name");
                XmlElement familyname = xmlDoc.CreateElement("familyname");
                XmlElement momos = xmlDoc.CreateElement("momos");

                XmlAttribute contactid = xmlDoc.CreateAttribute("id");
                contactid.Value = num.ToString();

                XmlText firtnametext = xmlDoc.CreateTextNode(tbName.Text);
                XmlText lastname = xmlDoc.CreateTextNode(tbFamilyName.Text);
                XmlCDataSection datasec = xmlDoc.CreateCDataSection(tbMomos.Text);

                contact.Attributes.Append(contactid);
                contact.AppendChild(name);
                contact.AppendChild(familyname);
                contact.AppendChild(momos);

                name.AppendChild(firtnametext);
                familyname.AppendChild(lastname);
                momos.AppendChild(datasec);

                xmlDoc.DocumentElement.AppendChild(contact);
                xmlDoc.Save(Application.StartupPath + "/contactsList.xml");

                tbFamilyName.Text = "";
                tbMomos.Text = "";
                tbName.Text = "";
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("" + ex.Message);
            //}

        }

Error: Object reference not set to an instance of an object.
What should I do. Where to use 'new' operator?

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.