Saturday, 30 August 2014

How to Write Data into XML Using Windows Forms Application in C#.Net

In this article i would like to explore a How to Write Data into XML Using Windows Forms Application in C#.Net.


For this We need to Follow the following  Steps
1.Take New Windows Forms Application
2.Design a Form as follows
3.Add The empty XML File
4.Now Implement the logic as follows




For all the 1 to 3 steps follow this link How To Read data From XML File Using Windows Forms Application Using C#.Net

Form should be as follows


Write Data into XML
Write Data into XML File

Write the following 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.Data.SqlClient;

namespace XML
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlConnection con;
        SqlDataAdapter da;
        DataSet ds;
        private void Form2_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Server=.;uid=sa;pwd=abc;database=employee");
            da = new SqlDataAdapter("select *from employee", con);
            ds = new DataSet();
            da.Fill(ds, "x");
            dataGridView1.DataSource = ds.Tables[0];
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "XML Files|*.Xml|All Files|*.*";
            openFileDialog1.ShowDialog();
            txtFileName.Text = openFileDialog1.FileName;
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            ds.WriteXml(txtFileName.Text);
            MessageBox.Show("Data has written into XML File", "Message");
        }
    }
}

Output:
Write Data into XML using C#.Net
Write Data into XML File
Now you can open and see your XML File it looks Like this

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <x>
    <empid>1</empid>
    <name>ravinn</name>
    <address>hyd</address>
    <designation>Devloper</designation>
    <salry>20000</salry>
    <createtime>2011-01-01T00:00:00+05:30</createtime>
  </x>
  <x>
    <empid>2</empid>
    <name>Ram</name>
    <address>KPHB</address>
    <designation>dsdddd</designation>
    <salry>1000</salry>
    <createtime>2011-01-01T00:00:00+05:30</createtime>
  </x>
  <x>
    <empid>4</empid>
    <name>abc</name>
    <address>abc</address>
    <designation>aba</designation>
    <salry>15230</salry>
    <createtime>2011-01-01T00:00:00+05:30</createtime>
  </x>
  <x>
    <empid>5</empid>
    <name>cbvv</name>
    <address>ghvghvghv</address>
    <designation>ghgvgvgh</designation>
    <salry>524545</salry>
    <createtime>2014-04-19T14:13:34.5+05:30</createtime>
  </x>
  <x>
    <empid>6</empid>
    <name>abc</name>
    <address>abc</address>
    <designation>abc</designation>
    <salry>50000</salry>
    <createtime>2014-04-19T14:13:45.467+05:30</createtime>
  </x>
  <x>
    <empid>7</empid>
    <name>Veerendra</name>
    <address>KPHB</address>
    <designation>SSE</designation>
    <salry>000000000000</salry>
    <createtime>2014-01-01T00:00:00+05:30</createtime>
  </x>
</NewDataSet>



No comments :

Post a Comment