Horizontal Nav

Tuesday, 15 July 2014

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:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
    <style type="text/css">  
        .auto-style1 {  
            text-align: right;  
            width: 75px;  
        }  
    </style>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>  
            <br />  
            <table style="width: 100%;">  
                <tr>  
                    <td class="auto-style1">Title:</td>  
                    <td>  
                        <asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>  
                    </td>  
                </tr>  
                <tr>  
                    <td class="auto-style1">Author:</td>  
                    <td>  
                        <asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>  
                    </td>  
                </tr>  
                <tr>  
                    <td class="auto-style1">Year:</td>  
                    <td>  
                        <asp:TextBox ID="tbYear" runat="server"></asp:TextBox> 
                    </td>  
                </tr>  
                <tr>  
                    <td class="auto-style1">Price:</td>  
                    <td>  
                        <asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>  
                    </td>  
                </tr>  
                <tr>  
                    <td class="auto-style1"> </td>  
                    <td>  
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  />  
                    </td>  
                </tr>  
            </table>  
            <asp:GridView ID="gvBookStoreRecords" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />  
                <EditRowStyle BackColor="#999999" />  
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />  
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />  
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />  
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />  
                <SortedAscendingCellStyle BackColor="#E9E7E2" />  
                <SortedAscendingHeaderStyle BackColor="#506C8C" />  
                <SortedDescendingCellStyle BackColor="#FFFDF8" />  
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />  
            </asp:GridView>  
        </div>  
    </form>  
</body>  
</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:

<?xmlversion="1.0"encoding="utf-8"?>  
<bookstore>  
  
</bookstore>
  
Step 3: On the click event of the btnSubmit (Button that we have placed at aspx page) write the following code:

protected void btnSubmit_Click(object sender, EventArgs e)
{  
    // creating object of XML DOCument class  
    XmlDocument XmlDocObj = new XmlDocument();  
    //loading XML File in memory  
    XmlDocObj.Load(Server.MapPath("BookStore.xml"));  
    //Select root node which is already defined  
    XmlNode RootNode = XmlDocObj.SelectSingleNode("bookstore");  
    //Creating one child node with tag name book  
    XmlNode bookNode = RootNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "book"""));  
    //adding node title to book node and inside it data taking from tbTitle TextBox  
      
    bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Title""")).InnerText = tbTitle.Text;  
    //adding node Author to book node and inside it data taking from tbAuthor TextBox  
    bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Author""")).InnerText = tbAuthor.Text;  
    //adding node Year to book node and inside it data taking from tbYear TextBox  
    bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Year""")).InnerText = tbYear.Text;  
    //adding node tbPrice to book node and inside it data taking from tbPrice TextBox  
    bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Price""")).InnerText = tbPrice.Text;  
  
    //after adding node, saving BookStore.xml back to the server  
    XmlDocObj.Save(Server.MapPath("BookStore.xml"));  
  
    //Calling grid view binding method.  
    gridDataBind();  
}  

Step 4: Create a gridDataBind Method to retrieve the data from the XML file:

public void gridDataBind()    
    {    
        DataSet ds = new DataSet();    
        //reading data from the BookStore.xml...    
        ds.ReadXml(Server.MapPath("BookStore.xml"));    
        //Adding dataset ds as a datasource of the grid view...    
        gvBookStoreRecords.DataSource = ds;    
        //binding data with grid view...    
        gvBookStoreRecords.DataBind();    
    }
     
Step 5: Now run the project and you will see the following outputs:

  1.  When the project loads the first time then:


After adding some data:





No comments:

Post a Comment