Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

XML Database In C#

0.00/5 (No votes)
6 Jul 2008 1  
This small application stores data in XML format

Introduction

This is a small application which stores the data in the form of XML and can be easily retrieved, inserted and deleted by using the ID.

2.JPG

Using the Code

Here is the code. You can use this application.

The code is not very difficult, I wrote it in less than 2 hours, whether or not you believe it.

// Insertion
         private void btnInsert_Click_1(object sender, EventArgs e)
        {
            if (txtBoxID.Text == "")
            {
                MessageBox.Show("Insert ID please");
                return;
            }

            XmlWriterSettings st = new XmlWriterSettings();
            st.Indent = true; ;

            st.OmitXmlDeclaration = true;
            st.Encoding = Encoding.ASCII;
            string path = @"ContactsDB\" + txtBoxID.Text.ToString() + ".xml";

            using (XmlWriter writer = XmlWriter.Create(path, st))
            {
                writer.WriteComment("This xml file for ID " + txtBoxID.Text + ".");
                writer.WriteStartElement("Contact");
                writer.WriteStartElement(lblID.Text);
                writer.WriteStartAttribute(lblID.Text);
                writer.WriteValue(txtBoxID.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();


                writer.WriteStartElement(lblName.Text);
                writer.WriteStartAttribute(lblName.Text);
                writer.WriteValue(txtBoxName.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                writer.WriteStartElement(lblFName.Text);
                writer.WriteStartAttribute(lblFName.Text);
                writer.WriteValue(txtBoxFname.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                writer.WriteStartElement(lblAdd.Text);
                writer.WriteStartAttribute(lblAdd.Text);
                writer.WriteValue(txtBoxAddress.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                writer.WriteStartElement(lblEmail.Text);
                writer.WriteStartAttribute(lblEmail.Text);
                writer.WriteValue(txtBoxEmail.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                writer.WriteStartElement(lblMob.Text);
                writer.WriteStartAttribute(lblMob.Text);
                writer.WriteValue(txtBoxMobile.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();

                writer.WriteStartElement(lblHome.Text);
                writer.WriteStartAttribute(lblHome.Text);
                writer.WriteValue(txtBoxHome.Text);
                writer.WriteEndAttribute();
                writer.WriteEndElement();
                writer.Flush();
                MessageBox.Show("DataBase Entry Successful!!");
                clearTxtBox();
                btnClearInsert.Enabled = true;
            }
            btnUpdate.Enabled = false;
        } 
           
// Deletion
             private void button2_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (txtDel.Text == "")
                {
                    MessageBox.Show("Insert ID please");
                    return;
                }
                string path = @"ContactsDB\" + txtDel.Text.ToString() + ".xml";
                File.Delete(path);
                MessageBox.Show("record for ID:" + txtDel.Text + " is deleted");
                txtDel.Text = "";
            }
            catch (Exception f)
            {
                MessageBox.Show("The desired record is not available");
            }
        }
        
//Search
        private void btnProcess_Click(object sender, EventArgs e)
        {
            try
            {
                string path = @"ContactsDB\" + txtQuery.Text.ToString() + ".xml";
                XmlDocument document = new XmlDocument();
                document.Load(path);
                XmlNode node = document.SelectSingleNode(@"//*");
                rtbResults.Text = node.OuterXml.ToString();
                btnUpdate.Enabled = true;
            }
            catch (Exception f)
            {
                MessageBox.Show("ID:" + txtQuery.Text + " not found");
                txtQuery.Text = "";
                rtbResults.Text = "";
            }
        }

History

  • 6th July, 2008: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here