How to Read, Edit, Update in XML using Windows Form in Asp.Net


Description:-

In this article we will see Update and Read Operation in Xml File using windows Form. Here we will design form for read record from xml file or if we want to edit record then also we can edit or update record in windows form in Xml file. It’s pretty easy to Read record and Update in Xml file same way we can delete or insert record in xml file. We will do that in next article. Here we will see read and update in xml File.

XML File:-

<?xml version="1.0" standalone="yes" ?>
<Customers>
  <Customer Id="1">
    <Name>Umesh Adroja</Name>
    <Email>Umesh@gmail.com</Email>
    <ZipCode>365420</ZipCode>
    <Country>India</Country>
    <State>Gujrat</State>
    <City>Ahmedabad</City>
  </Customer>
  <Customer Id="2">
    <Name>Chirag Patel</Name>
    <Email>Chirag@gmail.com</Email>
    <ZipCode>235689</ZipCode>
    <Country>India</Country>
    <State>Gujrat</State>
    <City>Rajkot</City>
  </Customer>
</Customers>

For Insert, Update and read Design your Windows form like below.

Now Go to Code behind file to Read, Update in Xml Using Windows Form. And Generate System.Xml Namespace. Create XML Node to Read Xml file from Directory.

int i;
XmlDocument myXMLDoc = new XmlDocument();
XmlNodeList MyName;
XmlNodeList MyEmail;
XmlNodeList MyZipCode;
XmlNodeList MyCountry;
XmlNodeList MyState;
XmlNodeList MyCity;

In Form Load event Read record from Xml file using XmlDocuments and Display in Textboxes.

public Form1()
{
  InitializeComponent();
  myXMLDoc.Load(@"C:\Users\UmesH\Desktop\Customers.xml");

  MyName = myXMLDoc.GetElementsByTagName("Name");
  MyEmail = myXMLDoc.GetElementsByTagName("Email");
  MyZipCode = myXMLDoc.GetElementsByTagName("ZipCode");
  MyCountry = myXMLDoc.GetElementsByTagName("Country");
  MyState = myXMLDoc.GetElementsByTagName("State");
  MyCity = myXMLDoc.GetElementsByTagName("City");

  i = Convert.ToInt16(txtCount.Text);

  txtName.Text = MyName[i - 1].InnerText;
  txtEmail.Text = MyEmail[i - 1].InnerText;
  txtZipCode.Text = MyZipCode[i - 1].InnerText;
  txtCountry.Text = MyCountry[i - 1].InnerText;
  txtState.Text = MyState[i - 1].InnerText;
  txtCity.Text = MyCity[i - 1].InnerText;
}

Now Generate Next button click event to read next record from Xml File like below and display in windows form.

private void btnNext_Click(object sender, EventArgs e)
{
  if (Convert.ToInt16(txtCount.Text) == MyName.Count)
  {
    MessageBox.Show("It's Last Record.", "Info", MessageBoxButtons.OK);
  }
  else
  {
    i = Convert.ToInt16(txtCount.Text);
    i = i + 1;
    txtCount.Text = i.ToString();

    txtName.Text = MyName[i - 1].InnerText;
    txtEmail.Text = MyEmail[i - 1].InnerText;
    txtZipCode.Text = MyZipCode[i - 1].InnerText;
    txtCountry.Text = MyCountry[i - 1].InnerText;
    txtState.Text = MyState[i - 1].InnerText;
    txtCity.Text = MyCity[i - 1].InnerText;
  }
}

Now Generate Previous button click event to read previous record from xml file like below.

private void btnPrevious_Click(object sender, EventArgs e)
{
  if (Convert.ToInt16(txtCount.Text) == 1)
  {
    MessageBox.Show("It's First Record.", "Info", MessageBoxButtons.OK);
  }
  else
  {
    i = Convert.ToInt16(txtCount.Text);
    i = i - 1;
    txtCount.Text = i.ToString();

    txtName.Text = MyName[i - 1].InnerText;
    txtEmail.Text = MyEmail[i - 1].InnerText;
    txtZipCode.Text = MyZipCode[i - 1].InnerText;
    txtCountry.Text = MyCountry[i - 1].InnerText;
    txtState.Text = MyState[i - 1].InnerText;
    txtCity.Text = MyCity[i - 1].InnerText;
  }
}

Now if we want to go first record then generate first button click event and code for go to first record directly.

private void btnFirst_Click(object sender, EventArgs e)
{
  i = 1;
  txtCount.Text = i.ToString();

  txtName.Text = MyName[i - 1].InnerText;
  txtEmail.Text = MyEmail[i - 1].InnerText;
  txtZipCode.Text = MyZipCode[i - 1].InnerText;
  txtCountry.Text = MyCountry[i - 1].InnerText;
  txtState.Text = MyState[i - 1].InnerText;
  txtCity.Text = MyCity[i - 1].InnerText;
}

Now same way to generate last button click event to go directly last record or read last record from xml file.

private void btnLast_Click(object sender, EventArgs e)
{
  i = MyName.Count;
  txtCount.Text = i.ToString();

  txtName.Text = MyName[i - 1].InnerText;
  txtEmail.Text = MyEmail[i - 1].InnerText;
  txtZipCode.Text = MyZipCode[i - 1].InnerText;
  txtCountry.Text = MyCountry[i - 1].InnerText;
  txtState.Text = MyState[i - 1].InnerText;
  txtCity.Text = MyCity[i - 1].InnerText;
}

Now generate Edit button click event to Edit record or enable record in form.

private void btnEdit_Click(object sender, EventArgs e)
{
  btnSave.Visible = true;
  txtName.Enabled = true;
  txtEmail.Enabled = true;
  txtZipCode.Enabled = true;
  txtCountry.Enabled = true;
  txtState.Enabled = true;
  txtCity.Enabled = true;
}

Now Generate Save button click event to save record in xml file and disable display record in windows form like below.

private void btnSave_Click(object sender, EventArgs e)
{
  i = Convert.ToInt16(txtCount.Text);
  MyName[i - 1].InnerText = txtName.Text;
  MyEmail[i - 1].InnerText = txtEmail.Text;
  MyZipCode[i - 1].InnerText = txtZipCode.Text;
  MyCountry[i - 1].InnerText = txtCountry.Text;
  MyState[i - 1].InnerText = txtState.Text;
  MyCity[i - 1].InnerText = txtCity.Text;

  myXMLDoc.Save(@"C:\Users\UmesH\Desktop\Customers.xml");
  if (MessageBox.Show("Save Successfully", "Confirm", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)
  {
    btnSave.Visible = false;
    txtName.Enabled = false;
    txtEmail.Enabled = false;
    txtZipCode.Enabled = false;
    txtCountry.Enabled = false;
    txtState.Enabled = false;
    txtCity.Enabled = false;
  }
}

Now you are to Read, Edit and Update record in Xml File simply run your Windows form.

Related Posts

Previous
Next Post »

Thanks for comments.....