XML Read & Update Operations

Let us see how the form actually looks:
 
01_Form_Look.png 
 
First of all let us see how to read values from a XML file.
  1. XmlDocument myXMLDoc = new XmlDocument();  
  2. myXMLDoc.Load(@"C:\Users\Indus_User\Desktop\books.xml");  
The above two lines will load the XML file in the given path.
  1. XmlNodeList myTitle;  
  2. XmlNodeList myFirstName;  
  3. XmlNodeList myLastName;  
  4. XmlNodeList myPrice;  
  5. myTitle = myXMLDoc.GetElementsByTagName("title");  
  6. myFirstName = myXMLDoc.GetElementsByTagName("first-name");  
  7. myLastName = myXMLDoc.GetElementsByTagName("last-name");  
  8. myPrice = myXMLDoc.GetElementsByTagName("price");  
The above code will store the values of each node given by the tab name into the related XML node list.
  1. i = Convert.ToInt16(txtCount.Text);  
  2. txtTitle.Text = myTitle[i - 1].InnerText;  
  3. txtFName.Text = myFirstName[i - 1].InnerText;  
  4. txtLName.Text = myLastName[i - 1].InnerText;  
  5. txtPrice.Text = myPrice[i - 1].InnerText; 
Using the above code, it will retrieve the particular index value & display into the related text boxes.
  1. if (Convert.ToInt16(txtCount.Text)== myTitle.Count)  
  2. {  
  3.     MessageBox.Show("It's Last Record.""Info", MessageBoxButtons.OK);  
  4. }  
  5. else  
  6. {  
  7.     i = Convert.ToInt32(txtCount.Text);  
  8.     i = i + 1;  
  9.     txtCount.Text = i.ToString();   
  10.     txtTitle.Text = myTitle[i-1].InnerText;  
  11.     txtFName.Text = myFirstName[i-1].InnerText;  
  12.     txtLName.Text = myLastName[i-1].InnerText;  
  13.     txtPrice.Text = myPrice[i-1].InnerText;  
  14. }  
When the user clicks on the next button (>>) then it will perform the above code & in that it will increase the index value & load the next indexed value into the related textboxes.
 
Here in the above code it will also check whether the index is the last index or not. If the index is last then it will display a message to the user.
 
See the following image for a better understanding:
 
02_Second_Record.png
 
07_Last_Record.png
  1. if (Convert.ToInt16(txtCount.Text)== 1)  
  2. {  
  3.     MessageBox.Show("It's First Record.""Info", MessageBoxButtons.OK);  
  4. }  
  5. else  
  6.     i = Convert.ToInt32(txtCount.Text);  
  7.     i = i - 1;  
  8.     txtCount.Text = i.ToString();  
  9.   
  10.     txtTitle.Text = myTitle[i-1].InnerText;  
  11.     txtFName.Text = myFirstName[i-1].InnerText;  
  12.     txtLName.Text = myLastName[i-1].InnerText;  
  13.     txtPrice.Text = myPrice[i-1].InnerText;  
  14. }  
When the user clicks on the previous button (<<) then it will perform the above code.
 
In this code it will decrease the index & then load the previous values into the related text boxes. If the index is the first then it will display a message to the user.
 
See the following image for a better understanding:
 
07_First_Record.png
  1. i = 1;  
  2. txtCount.Text = i.ToString();  
  3. txtTitle.Text = myTitle[i-1].InnerText;  
  4. txtFName.Text = myFirstName[i-1].InnerText;  
  5. txtLName.Text = myLastName[i-1].InnerText;  
  6. txtPrice.Text = myPrice[i-1].InnerText;  
When the user clicks on the First button (|<) then it will perform the above code.
 
In this code it will set the index as first 7 then it will load the related first record values into the text boxes.
  1. i = myTitle.Count;  
  2. txtCount.Text = i.ToString();  
  3. txtTitle.Text = myTitle[i-1].InnerText;  
  4. txtFName.Text = myFirstName[i-1].InnerText;  
  5. txtLName.Text = myLastName[i-1].InnerText;  
  6. txtPrice.Text = myPrice[i-1].InnerText; 
When the user clicks on the Last button (>|) then  it will perform the above code.
 
In this code it will set the index as last & then it will load the last records values into the related text boxes.
  1. btnSave.Visible = true;  
  2. txtTitle.Enabled = true;  
  3. txtFName.Enabled = true;  
  4. txtLName.Enabled = true;  
  5. txtPrice.Enabled = true;  
When the user clicks on the Edit button then it will display the Save button & also enabled all the text boxes to allow the user to edit the values.
 
See the following Image:
 
02_Edit_Button.png 
  1.  i = Convert.ToInt16(txtCount.Text);  
  2. myTitle[i - 1].InnerText = txtTitle.Text;  
  3. myFirstName[i - 1].InnerText = txtFName.Text;  
  4. myLastName[i - 1].InnerText = txtLName.Text;\  
  5. myPrice[i - 1].InnerText = txtPrice.Text;  
  6. myXMLDoc.Save(@"C:\Users\Indus_User\Desktop\books.xml");  
  7. if (MessageBox.Show("Save Successfully""Confirm", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)  
  8. {  
  9.     btnSave.Visible = false;  
  10.     txtTitle.Enabled = false;  
  11.     txtFName.Enabled = false;  
  12.     txtLName.Enabled = false;  
  13.     txtPrice.Enabled = false;  
  14. }  
When the user clicks on the save button then it will perform the above code.
 
In this code it will save the updated values into the related indexed values from the textboxes using the InnerText Property & then it will save the updated values into the XML file.
 
See the following Image:
 
04_Save_Button.png 
 
When the values are stored into the XML file then it will display a confirmation message to the user like below:
 
04_Save_Button.png 
 
When the user clicks on the OK button from the message box then it will hide the save button & also disable all the textboxes like in the following image:
 
06_After_Save.png 
 
Hope all this is clear for you.


Similar Articles