Performing Insert, Update, Delete in XML in ASP.Net

Introduction

This article explains the binding of a XML file in a Grid View and how to do insert, update and delete operations in the XML file.

Use the following procedure to create an ASP.NET Web Application in which we add a XML file first and then we'll bind the XML file to a Grid View and perform the IUD (Insert, Update, Delete) operations.

Step 1: Open Visual Studio.

Step 2: Create a new ASP.NET Web Application with the Empty Project Template.

Step 3: Add an XML file named MyXmlFile.xml.

Adding XML in Web Application

Step 4: Now create some content in the file as shown below:

  1. <?xml version="1.0" standalone="yes"?>  
  2. <StudentDetails>  
  3. <Student>  
  4. <ID>101</ID>  
  5. <Name>Amit</Name>  
  6. <Course>MCA</Course>  
  7. <College>Skyline</College>  
  8. </Student>  
  9. <Student>  
  10. <ID>102</ID>  
  11. <Name>Nimit</Name>  
  12. <Course>MCA</Course>  
  13. <College>Abes</College>  
  14. </Student>  
  15. </StudentDetails> 

Step 5: We'll now add a WebForm to our application by adding a new item to the project.

Adding Web Form

Step 6: Create a Grid View control in the body of your Web Form.

  1. <table>  
  2. <tr>  
  3. <td>  
  4. <asp:GridView ID="XmlGridView" runat="server" AutoGenerateColumns="false"  
  5. Height="247px" Width="795px" BackColor="White" BorderColor="#999999"  
  6. BorderStyle="None" BorderWidth="1px" CellPadding="3"  
  7. GridLines="Vertical" ShowFooter="true"  
  8. OnRowCancelingEdit="XmlGridView_RowCancelingEdit" OnRowDeleting="XmlGridView_RowDeleting"  
  9. OnRowEditing="XmlGridView_RowEditing" OnRowUpdating="XmlGridView_RowUpdating">  
  10. <AlternatingRowStyle BackColor="#DCDCDC" />  
  11. <Columns>  
  12. <asp:TemplateField HeaderText="ID">  
  13. <ItemTemplate>  
  14. <asp:Label ID="LblStuID" runat="server" Text='<%# Bind("ID")%>'></asp:Label>  
  15. </ItemTemplate>  
  16. <FooterTemplate>  
  17. <asp:TextBox ID="TxtStuID" runat="server"></asp:TextBox>  
  18. </FooterTemplate>  
  19. </asp:TemplateField>  
  20. <asp:TemplateField HeaderText="Name">  
  21. <ItemTemplate>  
  22. <asp:Label ID="LblStuName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>  
  23. </ItemTemplate>  
  24. <EditItemTemplate>  
  25. <asp:TextBox ID="TxtEditStuName" runat="server"></asp:TextBox>  
  26. </EditItemTemplate>  
  27. <FooterTemplate>  
  28. <asp:TextBox ID="TxtStuName" runat="server"></asp:TextBox>  
  29. </FooterTemplate>  
  30. </asp:TemplateField>  
  31. <asp:TemplateField HeaderText="Course">  
  32. <ItemTemplate>  
  33. <asp:Label ID="LblStuCourse" runat="server" Text='<%# Bind("Course") %>'></asp:Label>  
  34. </ItemTemplate>  
  35. <EditItemTemplate>  
  36. <asp:TextBox ID="TxtEditStuCourse" runat="server"></asp:TextBox>  
  37. </EditItemTemplate>  
  38. <FooterTemplate>  
  39. <asp:TextBox ID="TxtStuCourse" runat="server"></asp:TextBox>  
  40. </FooterTemplate>  
  41. </asp:TemplateField>  
  42. <asp:TemplateField HeaderText="College">  
  43. <ItemTemplate>  
  44. <asp:Label ID="LblStuCollege" runat="server" Text='<%# Bind("College") %>'></asp:Label>  
  45. </ItemTemplate>  
  46. <EditItemTemplate>  
  47. <asp:TextBox ID="TxtEditStuCollege" runat="server"></asp:TextBox>  
  48. </EditItemTemplate>  
  49. <FooterTemplate>  
  50. <asp:TextBox ID="TxtStuCollege" runat="server"></asp:TextBox>  
  51. </FooterTemplate>  
  52. </asp:TemplateField>  
  53. <asp:TemplateField HeaderText="Operations">  
  54. <ItemTemplate>  
  55. <asp:Button ID="BtnEdit" runat="server" CommandName="Edit" Text="Edit" />  
  56. <asp:Button ID="BtnDelete" runat="server" CommandName="Delete" Text="Delete" />  
  57. </ItemTemplate>  
  58. <EditItemTemplate>  
  59. <asp:Button ID="BthUpdate" runat="server" CommandName="Update" Text="Update" />  
  60. <asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" Text="Cancel" />  
  61. </EditItemTemplate>  
  62. <FooterTemplate>  
  63. <asp:Button ID="BtnInsert" runat="server" Text="Insert" OnClick="BtnInsert_Click" />  
  64. </FooterTemplate>  
  65. </asp:TemplateField>  
  66. </Columns>  
  67. <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  68. <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />  
  69. <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  70. <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
  71. <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  72. <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  73. <SortedAscendingHeaderStyle BackColor="#0000A9" />  
  74. <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  75. <SortedDescendingHeaderStyle BackColor="#000065" />  
  76. </asp:GridView>  
  77. </td>  
  78. </tr>  
  79. </table> 

Step 7: Press F7 to open the source code of the application.

Now, we need to bind the Grid View from our XML file to generate the data source. So, create a method named Get_Xml() and call the method in the Page_Load() event in your source code as shown below:

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack == true)  
  4.     {  
  5.         Get_Xml();  
  6.     }  
  7. }  
  8. void Get_Xml()  
  9. {  
  10.     DataSet ds = new DataSet();  
  11.     ds.ReadXml(Server.MapPath("~/MyXmlFile.xml"));  
  12.     if (ds != null && ds.HasChanges())  
  13.     {  
  14.         XmlGridView.DataSource = ds;  
  15.         XmlGridView.DataBind();  
  16.     }  
  17.     else  
  18.     {  
  19.         XmlGridView.DataBind();  
  20.     }  
  21. }   

Step 8: Press Ctrl+F5 to run the application.

Web Form

Step 9: Now, we perform the insert event by executing the code as shown below:

  1. protected void BtnInsert_Click(object sender, EventArgs e)  
  2. {  
  3.     Insert_XML();  
  4. }  
  5. void Insert_XML()  
  6. {  
  7.     TextBox Stu_Id = XmlGridView.FooterRow.FindControl("TxtStuID"as TextBox;  
  8.     TextBox Stu_Name = XmlGridView.FooterRow.FindControl("TxtStuName"as TextBox;  
  9.     TextBox Stu_Course = XmlGridView.FooterRow.FindControl("TxtStuCourse"as TextBox;  
  10.     TextBox Stu_College = XmlGridView.FooterRow.FindControl("TxtStuCollege"as TextBox;  
  11.     XmlDocument MyXmlDocument = new XmlDocument();  
  12.     MyXmlDocument.Load(Server.MapPath("~/MyXmlFile.xml"));  
  13.     XmlElement ParentElement = MyXmlDocument.CreateElement("Student");  
  14.     XmlElement ID = MyXmlDocument.CreateElement("ID");  
  15.     ID.InnerText = Stu_Id.Text;  
  16.     XmlElement Name = MyXmlDocument.CreateElement("Name");  
  17.     Name.InnerText = Stu_Name.Text;  
  18.     XmlElement Course = MyXmlDocument.CreateElement("Course");  
  19.     Course.InnerText = Stu_Course.Text;  
  20.     XmlElement College = MyXmlDocument.CreateElement("College");  
  21.     College.InnerText = Stu_College.Text;  
  22.     ParentElement.AppendChild(ID);  
  23.     ParentElement.AppendChild(Name);  
  24.     ParentElement.AppendChild(Course);  
  25.     ParentElement.AppendChild(College);  
  26.     MyXmlDocument.DocumentElement.AppendChild(ParentElement);  
  27.     MyXmlDocument.Save(Server.MapPath("~/MyXmlFile.xml"));  
  28.     Get_Xml();  
  29. }   

Step 10: Performing the Edit, Update and Delete operations:

  • Edit

    1. protected void XmlGridView_RowEditing(object sender, GridViewEditEventArgs e)  
    2. {  
    3.     XmlGridView.EditIndex = e.NewEditIndex;  
    4.     Get_Xml();  
    5. }  
    6. protected void XmlGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
    7. {  
    8.     XmlGridView.EditIndex = -1;  
    9.     Get_Xml();  
    10. }  

  • Update

    1. protected void XmlGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)  
    2. {  
    3.     int id = XmlGridView.Rows[e.RowIndex].DataItemIndex;  
    4.     TextBox Name = XmlGridView.Rows[e.RowIndex].FindControl("TxtEditStuName"as TextBox;  
    5.     TextBox Course = XmlGridView.Rows[e.RowIndex].FindControl("TxtEditStuCourse"as TextBox;  
    6.     TextBox College = XmlGridView.Rows[e.RowIndex].FindControl("TxtEditStuCollege"as TextBox;  
    7.     XmlGridView.EditIndex = -1;  
    8.     Get_Xml();  
    9.     DataSet ds = XmlGridView.DataSource as DataSet;  
    10.     ds.Tables[0].Rows[id]["Name"] = Name.Text;  
    11.     ds.Tables[0].Rows[id]["Course"] = Course.Text;  
    12.     ds.Tables[0].Rows[id]["College"] = College.Text;  
    13.     ds.WriteXml(Server.MapPath("~/MyXmlFile.xml"));  
    14.     Get_Xml();  
    15. }

  • Delete

    1. protected void XmlGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)  
    2. {  
    3.     Get_Xml();  
    4.     DataSet ds = XmlGridView.DataSource as DataSet;  
    5.     ds.Tables[0].Rows[XmlGridView.Rows[e.RowIndex].DataItemIndex].Delete();  
    6.     ds.WriteXml(Server.MapPath("~/MyXmlFile.xml"));  
    7.     Get_Xml();  
    8. }  

     

Step 11: Now you can perform the Edit, Update and Delete operations with the events shown above.

  • Edit

    Edit in the Gird View

  • Update

    Update in the Grid View

  • Delete

    Delete in the Grid View

Updated XML File

Updated XML File

Summary

With this article, you can bind a Grid View with the newly created XML file. You can also perform the Insert, Update and Delete operations in the Grid View as well as the changes made in the XML file also. Happy Coding!!


Similar Articles