Wednesday 11 April 2012

How to insert data into xml file from windows form using C#.Net || Insert and Retrieve data from xml file to bind datagridview.

To implement this concept you need to follow the below steps : 

Step1:
On the File menu, click New Project. Select Windows Forms Application as your project type.
Design the Form using controls from Toolbox.

Step2:
Now open the Form.cs page and write the following source code.

Form1.cs Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace save_data_to_xml
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string PATH = "xmldata.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");
                XmlElement root = doc.CreateElement("Persons");
                XmlElement person = doc.CreateElement("Person");
                XmlAttribute Name = doc.CreateAttribute("Name");
                XmlElement Designation = doc.CreateElement("Designation");
                XmlElement Location = doc.CreateElement("Location");

                //Add the values for each nodes
                Name.Value = txtName.Text;
                Designation.InnerText = txtDesignation.Text;
                Location.InnerText = txtLocation.Text;

                //Construct the document
                doc.AppendChild(declaration);
                doc.AppendChild(comment);
                doc.AppendChild(root);
                root.AppendChild(person);
                person.Attributes.Append(Name);
                person.AppendChild(Designation);
                person.AppendChild(Location);

                doc.Save(PATH);
            }
            else //If there is already a file
            {
                //Load the XML File
                doc.Load(PATH);

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

                XmlElement person = doc.CreateElement("Person");
                XmlAttribute Name = doc.CreateAttribute("Name");
                XmlElement Designation = doc.CreateElement("Designation");
                XmlElement Location = doc.CreateElement("Location");

                //Add the values for each nodes
                Name.Value = txtName.Text;
                Designation.InnerText = txtDesignation.Text;
                Location.InnerText = txtLocation.Text;

                //Construct the Person element
                person.Attributes.Append(Name);
                person.AppendChild(Designation);
                person.AppendChild(Location);

                //Add the New person element to the end of the root element
                root.AppendChild(person);

                //Save the document
                doc.Save(PATH);
            }

            //Show confirmation message
            MessageBox.Show("Details have been added to the XML File.");

            //Reset text fields for new input
            txtName.Text = String.Empty;
            txtDesignation.Text = String.Empty;
            txtLocation.Text = String.Empty;
        }

        private void btnview_Click(object sender, EventArgs e)
        {
            try
            {
                XmlReader xmlFile;
                xmlFile = XmlReader.Create("xmldata.xml", new XmlReaderSettings());
                DataSet ds = new DataSet();
                ds.ReadXml(xmlFile);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}



Step3:
Now build the Solution and Debug it for the output.

Output :


Xml Output file :


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--This is an XML Generated File-->
<Persons>
  <Person Name="sridhar">
    <Designation>developer</Designation>
    <Location>hyderabad</Location>
  </Person>
<Person Name="karthik">
    <Designation>snr devoloper</Designation>
    <Location>Ameerpet</Location>
  </Person>
</Persons>



2 comments: