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.

XmlDocument myXMLDoc = new XmlDocument();

myXMLDoc.Load(@"C:\Users\Indus_User\Desktop\books.xml");


The above two lines will load the XML file in the given path.
 
 XmlNodeList myTitle;
 XmlNodeList myFirstName;
 XmlNodeList myLastName;
 XmlNodeList myPrice;

myTitle = myXMLDoc.GetElementsByTagName("title");
myFirstName = myXMLDoc.GetElementsByTagName("first-name");
myLastName = myXMLDoc.GetElementsByTagName("last-name");
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.
 
 i = Convert.ToInt16(txtCount.Text);
 txtTitle.Text = myTitle[i - 1].InnerText;
 txtFName.Text = myFirstName[i - 1].InnerText;
 txtLName.Text = myLastName[i - 1].InnerText;
 txtPrice.Text = myPrice[i - 1].InnerText;

Using the above code, it will retrieve the particular index value & display into the related text boxes.
 
if (Convert.ToInt16(txtCount.Text)== myTitle.Count)
{
                MessageBox.Show("It's Last Record.", "Info", MessageBoxButtons.OK);
}
else
{
                i = Convert.ToInt32(txtCount.Text);
                i = i + 1;
                txtCount.Text = i.ToString(); 
                txtTitle.Text = myTitle[i-1].InnerText;
                txtFName.Text = myFirstName[i-1].InnerText;
                txtLName.Text = myLastName[i-1].InnerText;
                txtPrice.Text = myPrice[i-1].InnerText;
}

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

 
 if (Convert.ToInt16(txtCount.Text)== 1)
{
                MessageBox.Show("It's First Record.", "Info", MessageBoxButtons.OK);
}
else

                i = Convert.ToInt32(txtCount.Text);
                i = i - 1;
                txtCount.Text = i.ToString();

                txtTitle.Text = myTitle[i-1].InnerText;
                txtFName.Text = myFirstName[i-1].InnerText;
                txtLName.Text = myLastName[i-1].InnerText;
                txtPrice.Text = myPrice[i-1].InnerText;
}


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
 
 i = 1;
 txtCount.Text = i.ToString();
 txtTitle.Text = myTitle[i-1].InnerText;
 txtFName.Text = myFirstName[i-1].InnerText;
 txtLName.Text = myLastName[i-1].InnerText;
 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.
 
 i = myTitle.Count;
 txtCount.Text = i.ToString();
 txtTitle.Text = myTitle[i-1].InnerText;
 txtFName.Text = myFirstName[i-1].InnerText;
 txtLName.Text = myLastName[i-1].InnerText;
 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.
 
 btnSave.Visible = true;
txtTitle.Enabled = true;
txtFName.Enabled = true;
txtLName.Enabled = true;
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
 
 i = Convert.ToInt16(txtCount.Text);
myTitle[i - 1].InnerText = txtTitle.Text;
myFirstName[i - 1].InnerText = txtFName.Text;
myLastName[i - 1].InnerText = txtLName.Text;\
myPrice[i - 1].InnerText = txtPrice.Text;

myXMLDoc.Save(@"C:\Users\Indus_User\Desktop\books.xml");
if (MessageBox.Show("Save Successfully", "Confirm", MessageBoxButtons.OK) == System.Windows.Forms.DialogResult.OK)
{
                btnSave.Visible = false;
                txtTitle.Enabled = false;
                txtFName.Enabled = false;
                txtLName.Enabled = false;
                txtPrice.Enabled = false;
}
 


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.

Up Next
    Ebook Download
    View all
    Learn
    View all