To insert data into the xml file in ASP.Net we should have follow the following steps:
1) Open Visual Studio
2) Go to File=>New=>Web Site( give the name as insertdataxml)
3) Add a new webform into the project named it as:(Dataxml.aspx) design it as following:
4)Add a Xml file into the project and name it as ("EmployeeData.Xml") and add the following into the xml file:
<employeeinformation>
</employeeinformation>
5)Double click on the Submit button and write the following:
using System.Xml;
using System.Data;
namespace Gridview_with_checkbox
{
public partial class Dataxml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ShowFileData();
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("EmployeeData.xml"));
XmlElement ParentElement = xmldoc.CreateElement("Employee");
XmlElement name = xmldoc.CreateElement("Name");
name.InnerText = txtename.Text;
XmlElement job = xmldoc.CreateElement("Job");
job.InnerText = txtempjob.Text;
XmlElement sal = xmldoc.CreateElement("Sal");
sal.InnerText = txtempsal.Text;
ParentElement.AppendChild(name);
ParentElement.AppendChild(job);
ParentElement.AppendChild(sal);
xmldoc.DocumentElement.AppendChild(ParentElement);
xmldoc.Save(Server.MapPath("EmployeeData.xml"));
ShowFileData();
}
private void ShowFileData()
{
using(DataSet ds=new DataSet ())
{
ds.ReadXml(Server.MapPath("~/EmployeeData.xml"));
if(ds.Tables.Count>0)
{
gview1.DataSource = ds.Tables[0];
gview1.DataBind();
}
}
}
}
}
5)Now run the page and enter the data into the textboxes and click on the submit button then you will see the data should be inserted into the xml file.







Very good
ReplyDelete