Introduction: This article explains how to use an 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 an XML file.
Step 1: Create an ASP.NET project add one 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)
- {
-
- XmlDocument XmlDocObj = new XmlDocument();
-
- XmlDocObj.Load(Server.MapPath("BookStore.xml"));
-
- XmlNode RootNode = XmlDocObj.SelectSingleNode("bookstore");
-
- XmlNode bookNode = RootNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "book", ""));
-
-
- bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Title", "")).InnerText = tbTitle.Text;
-
- bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Author", "")).InnerText = tbAuthor.Text;
-
- bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Year", "")).InnerText = tbYear.Text;
-
- bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Price", "")).InnerText = tbPrice.Text;
-
-
- XmlDocObj.Save(Server.MapPath("BookStore.xml"));
-
-
- gridDataBind();
- }
Step 4: Create a
gridDataBind Method to retrieve the data from the XML file:
- public void gridDataBind()
- {
- DataSet ds = new DataSet();
-
- ds.ReadXml(Server.MapPath("BookStore.xml"));
-
- gvBookStoreRecords.DataSource = ds;
-
- gvBookStoreRecords.DataBind();
- }
Step 5: Now run the project and you will see the following outputs:
- When the project loads the first time then:
- After adding some data: