Sunday, March 24, 2019

Insert, Update and Delete in XML File Using C#

Insert, Update and Delete in XML File Using C#


This article explains how to do insert, update and delete operations in a XML file using C#.

Use the following procedure to create a sample of doing that in XML using C#.

Step 1: Create a solution.
Create a solution

Step 2: Add a web application.
 
Add a web application

Step 3: Right-click the web application and add the XML file. The solution will be as shown below:
 
xml file

Step 4: Open the XML file and the fields as below:
 
<?xml version="1.0" standalone="yes"?>
<Employees>
  <Employee>
    <ID>101</ID>
    <Name>Abhay</Name>
    <Designation>Senior Software Engineer</Designation>
    <EmailID>abhay.shanker@hotmail.com</EmailID>
    <City>Pune</City>
    <Country>India</Country>
    <Technology>.Net</Technology>
  </Employee>
</Employees>  
Step 5: Go to the .aspx page and design the form as below:
design the form

Step 6: Go to the .aspx.cs page and write the code for the add, update and delete in XML.
To add the record write the code as:
XmlDocument xmlEmloyeeDoc = new XmlDocument();
xmlEmloyeeDoc.Load(Server.MapPath("~/Employees.xml"));
XmlElement ParentElement = xmlEmloyeeDoc.CreateElement("Employee");
XmlElement ID = xmlEmloyeeDoc.CreateElement("ID");
ID.InnerText = txtID.Text;
XmlElement Name = xmlEmloyeeDoc.CreateElement("Name");
Name.InnerText = txtName.Text;
XmlElement Designation = xmlEmloyeeDoc.CreateElement("Designation");
Designation.InnerText = txtDesignation.Text;
XmlElement EmailID = xmlEmloyeeDoc.CreateElement("EmailID");
EmailID.InnerText = txtEmailID.Text;
XmlElement City = xmlEmloyeeDoc.CreateElement("City");
City.InnerText = txtCity.Text;
XmlElement Country = xmlEmloyeeDoc.CreateElement("Country");
Country.InnerText = txtCountry.Text;
XmlElement Technology = xmlEmloyeeDoc.CreateElement("Technology");

Technology.InnerText = txtTechnology.Text;
ParentElement.AppendChild(ID);
ParentElement.AppendChild(Name);
ParentElement.AppendChild(Designation);
ParentElement.AppendChild(EmailID);
ParentElement.AppendChild(City);
ParentElement.AppendChild(Country);
ParentElement.AppendChild(Technology);
xmlEmloyeeDoc.DocumentElement.AppendChild(ParentElement);
xmlEmloyeeDoc.Save(Server.MapPath("~/Employees.xml"));
BindGrid();  
To update the record write the code as:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Employees.xml"));
int xmlRow = Convert.ToInt32(Convert.ToString(ViewState["gridrow"]));
ds.Tables[0].Rows[xmlRow]["Name"] = txtName.Text;
ds.Tables[0].Rows[xmlRow]["Designation"] = txtDesignation.Text;
ds.Tables[0].Rows[xmlRow]["EmailID"] = txtEmailID.Text;
ds.Tables[0].Rows[xmlRow]["City"] = txtCity.Text;
ds.Tables[0].Rows[xmlRow]["Country"] = txtCountry.Text;
ds.Tables[0].Rows[xmlRow]["Technology"] = txtTechnology.Text;
ds.WriteXml(Server.MapPath("~/Employees.xml"));
BindGrid();
 
To delete the record write the code as:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Employees.xml"));
ds.Tables[0].Rows.RemoveAt(e.RowIndex);
ds.WriteXml(Server.MapPath("~/Employees.xml"));
BindGrid();
For binding the Grid:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Employees.xml"));
if (ds != null && ds.HasChanges())
{
    grdxml.DataSource = ds;
    grdxml.DataBind();
}
else
{
    grdxml.DataBind();
}

No comments:

Post a Comment

No String Argument Constructor/Factory Method to Deserialize From String Value

  In this short article, we will cover in-depth the   JsonMappingException: no String-argument constructor/factory method to deserialize fro...