How to Use XML File to Store Data and Retrieve Data Using ASP.Net With C#

Introduction: This article explains how to use a XML file to store and retrieve data dynamically using ASP.NET with C#.  This article will also help to create child nodes in a XML file using ASP.NET. 
 
Use the following procedure to create a simple application (BookStore) for storing data to and retrieving data from a XML file.
 
Step 1: Create an ASP.NET project add one web web form and add the following controls:

Control Id

Control

Purpose

tbTitle

TextBox Control

To enter the title of the book.

tbAuthor

TextBox Control

To enter the Author Name of the book.

tbYear

TextBox Control

To enter the publishing year of the book.

tbPrice

TextBox Control

To enter the price of the book.

btnSubmit

Button Control

To Submit the data.

gvBookStoreRecords

GridView Control

To display data of XML File


I have designed my aspx page like as in the following:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .auto-style1 {  
  10.             text-align: right;  
  11.             width: 75px;  
  12.         }  
  13.     </style>  
  14. </head>  
  15. <body>  
  16.     <form id="form1" runat="server">  
  17.         <div>  
  18.             <br />  
  19.             <table style="width: 100%;">  
  20.                 <tr>  
  21.                     <td class="auto-style1">Title:</td>  
  22.                     <td>  
  23.                         <asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>  
  24.                     </td>  
  25.                 </tr>  
  26.                 <tr>  
  27.                     <td class="auto-style1">Author:</td>  
  28.                     <td>  
  29.                         <asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>  
  30.                     </td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td class="auto-style1">Year:</td>  
  34.                     <td>  
  35.                         <asp:TextBox ID="tbYear" runat="server"></asp:TextBox>  
  36.                     </td>  
  37.                 </tr>  
  38.                 <tr>  
  39.                     <td class="auto-style1">Price:</td>  
  40.                     <td>  
  41.                         <asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>  
  42.                     </td>  
  43.                 </tr>  
  44.                 <tr>  
  45.                     <td class="auto-style1"> </td>  
  46.                     <td>  
  47.                         <asp:Button ID="btnSubmit" runat="server" Text="Submit"  />  
  48.                     </td>  
  49.                 </tr>  
  50.             </table>  
  51.             <asp:GridView ID="gvBookStoreRecords" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
  52.                 <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
  53.                 <EditRowStyle BackColor="#999999" />  
  54.                 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  55.                 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
  56.                 <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
  57.                 <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
  58.                 <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
  59.                 <SortedAscendingCellStyle BackColor="#E9E7E2" />  
  60.                 <SortedAscendingHeaderStyle BackColor="#506C8C" />  
  61.                 <SortedDescendingCellStyle BackColor="#FFFDF8" />  
  62.                 <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
  63.             </asp:GridView>  
  64.         </div>  
  65.     </form>  
  66. </body>  
  67. </html>  

And as in the code above I designed the page as in the following:



Step 2
: Add an XML file within the project solution, provide it the name BookStore.xml. Create a root node in it as in the following:
  1. <?xmlversion="1.0"encoding="utf-8"?>  
  2. <bookstore>  
  3.   
  4. </bookstore>  
Step 3: On the click event of the btnSubmit (Button that we have placed at aspx page) write the following code:
  1. protected void btnSubmit_Click(object sender, EventArgs e)  
  2. {  
  3.     // creating object of XML DOCument class  
  4.     XmlDocument XmlDocObj = new XmlDocument();  
  5.     //loading XML File in memory  
  6.     XmlDocObj.Load(Server.MapPath("BookStore.xml"));  
  7.     //Select root node which is already defined  
  8.     XmlNode RootNode = XmlDocObj.SelectSingleNode("bookstore");  
  9.     //Creating one child node with tag name book  
  10.     XmlNode bookNode = RootNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "book"""));  
  11.     //adding node title to book node and inside it data taking from tbTitle TextBox  
  12.       
  13.     bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Title""")).InnerText = tbTitle.Text;  
  14.     //adding node Author to book node and inside it data taking from tbAuthor TextBox  
  15.     bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Author""")).InnerText = tbAuthor.Text;  
  16.     //adding node Year to book node and inside it data taking from tbYear TextBox  
  17.     bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Year""")).InnerText = tbYear.Text;  
  18.     //adding node tbPrice to book node and inside it data taking from tbPrice TextBox  
  19.     bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Price""")).InnerText = tbPrice.Text;  
  20.   
  21.     //after adding node, saving BookStore.xml back to the server  
  22.     XmlDocObj.Save(Server.MapPath("BookStore.xml"));  
  23.   
  24.     //Calling grid view binding method.  
  25.     gridDataBind();  
  26. }  
Step 4: Create a gridDataBind Method to retrieve the data from the XML file:
  1. public void gridDataBind()    
  2.     {    
  3.         DataSet ds = new DataSet();    
  4.         //reading data from the BookStore.xml...    
  5.         ds.ReadXml(Server.MapPath("BookStore.xml"));    
  6.         //Adding dataset ds as a datasource of the grid view...    
  7.         gvBookStoreRecords.DataSource = ds;    
  8.         //binding data with grid view...    
  9.         gvBookStoreRecords.DataBind();    
  10.     }     
Step 5: Now run the project and you will see the following outputs:
  1.  When the project loads the first time then:

  2. After adding some data:


Up Next
    Ebook Download
    View all
    Learn
    View all
    sourabhsomani.com