How to insert data into xml file using Windows forms application in C# || Insert and Retrieve data from xml file to bind datagridview in Windows Forms C#

Insert data into XML file :

This article is shows about insert data into XML file instead of using SqlServer database.
For that first Design your form as per your requirements.
In this am adding daily expenses of a person for that am taking fields as Date,  Amount, Purpose, Source  Bank and one Save button like below:

For accessing XML file add the following namespace in top of your code:

                         using System.Xml;

Write the following code in your Save button Click:
private void btnsave_Click(object sender, EventArgs e)

            string path = "Expences.xml";
            XmlDocument doc = new XmlDocument();

            //If there is no current file, then create a new one

            if (!System.IO.File.Exists(path))
                //Create neccessary nodes
                XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlComment comment = doc.CreateComment("This is an XML Generated File");

            else //If there is already a file
                //    //Load the XML File

            //Get the root element
            XmlElement root = doc.DocumentElement;

            XmlElement Subroot = doc.CreateElement("Expences");
            XmlElement Date = doc.CreateElement("Date");
            XmlElement Amount = doc.CreateElement("Amount");
            XmlElement Purpose = doc.CreateElement("purpose");
            XmlElement Source = doc.CreateElement("Source");
            XmlElement Bank = doc.CreateElement("Bank");

            //Add the values for each nodes
            Date.InnerText = Convert.ToDateTime(dateTimePicker1.Text).ToShortDateString();
            Amount.InnerText = txtAmount.Text;
            Purpose.InnerText = txtPurpose.Text;
            if (radioButton1.Checked)
                Source.InnerText = "Cash";
                Bank.InnerText = " ";
                Source.InnerText = "Bank";
                Bank.InnerText = comboBox1.SelectedItem.ToString();



            //Save the document

            //Show confirmation message
            MessageBox.Show("Details  added Successfully");

            //Reset text fields for new input
            txtPurpose.Text = String.Empty;
            txtAmount.Text = String.Empty;
      Then run your application and see output like below:

To see your saving data in XML file do the following steps:
Select your solution from Solution Explorer
Right click and Select Open Folder in Windows Explorer
Then your application folder will open in that double click on bin folder  then double click debug folder
Then you will see your Expenses XML document file à double click on it then you will see your Saving Data in your browser like below:

Retrieve data from XML file and show in DataGridview :
For showing these saving data into DataGridview, am taking one Show button and DataGridview Control in the form:
Write the following code in show button click:
     private void btnShow_Click(object sender, EventArgs e)
                XmlReader xmlFile;
                xmlFile = XmlReader.Create("Expences.xml"new XmlReaderSettings());
                DataSet ds = new DataSet();
                dataGridView1.DataSource = ds.Tables[0];

Then you will see your data binding to DataGridview like below:

1 comment:

  1. the play button is not working

    plz see the issue...

    which is help to viewer....