Monday, 29 September 2014

How to Save Record in XML File and Read XML to Display in DataGridview Using C#.net in Windows Application

9/29/2014 - By Pranav Singh 0

This article will show you how you can save the record in XML file and read xm l to display in datagridview using c#.net in windows application.


So for this article first we will create a new windows application and in this application we will add some controls in it. Now we will add a folder and create an XML file in it.




Your form will look as shown below.


Now on form code we will add the below code.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void GetData()
        {
            try
            {
                DataSet dsResult = new DataSet();
                dsResult.ReadXml("StudentXML/StudentXML.xml");
                if (dsResult.Tables.Count != 0)
                {
                    if (dsResult.Tables[0].Rows.Count > 0)
                    {
                        dataGridView1.DataSource = dsResult.Tables[0];
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*Save*/
            using (DataSet dsResult = new DataSet())
            {
                dsResult.ReadXml("StudentXML/StudentXML.xml");
                if (dsResult.Tables.Count == 0)
                {
                    StudentModel objStudentModel = new StudentModel();
                    objStudentModel.Id = Convert.ToInt32(txtid.Text);
                    objStudentModel.Name = txtname.Text;
                    objStudentModel.Address = txtaddress.Text;

                    XmlTextWriter writer = new XmlTextWriter("StudentXML/StudentXML.xml", System.Text.Encoding.UTF8);

                    writer.WriteStartDocument(true);
                    writer.Formatting = Formatting.Indented;
                    writer.Indentation = 2;
                    writer.WriteStartElement("Student");

                    writer.WriteStartElement("Student");
                    writer.WriteStartElement("Id");
                    writer.WriteString(objStudentModel.Id.ToString());
                    writer.WriteEndElement();
                    writer.WriteStartElement("Name");
                    writer.WriteString(objStudentModel.Name);
                    writer.WriteEndElement();
                    writer.WriteStartElement("Address");
                    writer.WriteString(objStudentModel.Address);
                    writer.WriteEndElement();
                    writer.WriteEndElement();

                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Close();
                    dsResult.ReadXml("StudentXML/StudentXML.xml");
                }
                else
                {
                    //-- Add one new row in to dataset and set the column data accordingly.
                    dsResult.Tables[0].Rows.Add(dsResult.Tables[0].NewRow());
                    dsResult.Tables[0].Rows[dsResult.Tables[0].Rows.Count - 1]["Id"] = txtid.Text;
                    dsResult.Tables[0].Rows[dsResult.Tables[0].Rows.Count - 1]["Name"] = txtname.Text.ToUpper();
                    dsResult.Tables[0].Rows[dsResult.Tables[0].Rows.Count - 1]["Address"] = txtaddress.Text;
                    dsResult.AcceptChanges();
                    //-- Write final data to XML file using write method
                    dsResult.WriteXml("StudentXML/StudentXML.xml", XmlWriteMode.IgnoreSchema);
                }
                dataGridView1.DataSource = dsResult.Tables[0];
                MessageBox.Show("Data Saved Successfully.");
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

In above code I have created a function in which I have read the xml file and bind it to datagridview. Now on button click event I have first added the stating node and then updated the record and put it in XML file.

Now we have done run the application to check the output.



Now click on save



 Here is your XML file

xml version="1.0" standalone="yes"?>
<Student>
  <Student>
    <Id>1</Id>
    <Name>Rajesh</Name>
    <Address>Address1</Address>
  </Student>
  <Student>
    <Id>2</Id>
    <Name>SHYAM</Name>
    <Address>Address2</Address>
  </Student>
  <Student>
    <Id>3</Id>
    <Name>VINAY</Name>
    <Address>Address3</Address>
  </Student>
  <Student>
    <Id>4</Id>
    <Name>PRATIMA</Name>
    <Address>Address4</Address>
  </Student>
</Student>

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top