Wednesday 11 April 2012

Storing values into XMlfile and display values from Xml File in gridview


How to store values in XML-file and display them in grid view

Step 1:
Create a new Windows Forms Application and create a form.Design the form as below


Give the name of the text boxes as textBoxName, textBoxAge, and textBoxGender.Give the name of the button as buttonAdd.

Step 2
Now go to Form.cs add a namespace on top of code
using System.Xml;
Under class declaration add a private XmlDocument member and a string indicating the path of the XML file that we will use throughout the code
private XmlDocument doc;
 private const string PATH = "sample.xml";

Step 3
Go back to the designer and double click the add button to generate an event handler for its Click event then use the following code.
private void buttonAdd_Click(object sender, EventArgs e)
        {
            doc = new XmlDocument();

 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 age = doc.CreateElement("Age");
        XmlElement gender = doc.CreateElement("Gender");

        //Add the values for each nodes
        name.Value = textBoxName.Text;
        age.InnerText = textBoxAge.Text;
        gender.InnerText = textBoxGender.Text;

        //Construct the document
        doc.AppendChild(declaration);
        doc.AppendChild(comment);
        doc.AppendChild(root);
        root.AppendChild(person);
        person.Attributes.Append(name);
        person.AppendChild(age);
        person.AppendChild(gender);

        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 age = doc.CreateElement("Age");
        XmlElement gender = doc.CreateElement("Gender");

        //Add the values for each nodes
        name.Value = textBoxName.Text;
        age.InnerText = textBoxAge.Text;
        gender.InnerText = textBoxGender.Text;

        //Construct the Person element
        person.Attributes.Append(name);
        person.AppendChild(age);
        person.AppendChild(gender);

        //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
    textBoxName.Text = String.Empty;
    textBoxAge.Text = String.Empty;
    textBoxGender.Text = String.Empty;

        }

Step 4
Now build and run the solution


In order Retrieve and display the data stored in Xml File  in a table form continue with below procedure

Step 5
Go back to the designer and add a button and datagridview ,name the button as View .



Step 6

Double click the view button to generate an event handler for its Click event then use the following code.


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

Step 7
Now build and run the solution



No comments:

Post a Comment