SIGN UP MEMBER LOGIN:    
ARTICLE

XML Read & Update Operations

Posted by Ghanashyam Nayak Articles | XML in C# February 08, 2012
Using this article you can learn how to read each XML node value & how to update that value in the original XML file...
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 

You are always welcome Sumita...

Posted by Ghanashyam Nayak Feb 09, 2012

Thank you so much Daisy..

Posted by Ghanashyam Nayak Feb 09, 2012

Thank You So Much Laura...

Posted by Ghanashyam Nayak Feb 09, 2012

Thanx Abhi & you are welcome...

Posted by Ghanashyam Nayak Feb 09, 2012

Thank You Nitin...

Posted by Ghanashyam Nayak Feb 09, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor