Update XML Node value using C#

Update the XML document by passing the value to it, updating the specific node of the XML document and replacing the existing value.

Here I am updating the date node in the XML document that will be updated while I have entered the text and click the button


update XML using C#

XML format : XMLFile1.xml

<All_batches>
  <Date>10-jan-2016</Date>

</All_batches>

C#

using System;
using System.Web;
using System.Xml;

namespace XMLINS
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("D:/WEBROOT/XMLINS/XMLINS/XMLFile1.xml");

            XmlNodeList aDateNodes = doc.SelectNodes("/All_batches/Date");
            foreach (XmlNode aDateNode in aDateNodes)
            {
                XmlAttribute DateAttribute = aDateNode.Attributes["Date"];
                aDateNode.InnerText = TextBox1.Text;
            }
            doc.Save("D:/WEBROOT/XMLINS/XMLINS/XMLFile1.xml");
            Label1.Text = "XML updated Successfully";
        }
    }
}