Working With XML File in ASP.NET

Introduction

Extensible Markup Language (XML) strores and transports data. If we use a XML file to store the data then we can do operations with the XML file directly without using the database. The XML format is supported for all applications. It is independent of all software applications and it is accessible by all applications. Here is an example that shows how to insert the data into the XML file and how to retrieve the data from the XML file; also how to bind the data into tha DataList using ASP.NET. For this the following procedure can be used.

Step 1: First start Visual Studio 2010 by clicking "File" -> "New" -> "Web Site..." then click on "ASP.NET Web Site". Now write the following inline code that will design your web page.

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

   <title></title>

   <style type="text/css">

        .style1

        {

           width: 40%;

           border-style: solid;

           border-width: 1px;

           background-color:Silver;

           height: 152px;

        }

        .style2

        {

           width: 295px;

        }

        .style3

        {

           width: 754px;

        }

   </style>

</head>

<body>

   <form id="form1" runat="server">

   <div>

       <table class="style1">

           <tr>

               <td class="style2">

                   &nbsp;</td>

               <td class="style3">

                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Employee_Information</td>

           </tr>

           <tr>

               <td class="style2">

                    Name:</td>

               <td class="style3">

                   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

               </td>

           </tr>

           <tr>

               <td class="style2">

                    Emp_Id:</td>

               <td class="style3">

                   <asp:TextBox ID="TextBox2"runat="server"></asp:TextBox>

               </td>

           </tr>

           <tr>

                <td class="style2">

                    Qualification:</td>

               <td class="style3">

                   <asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="132px">

                       <asp:ListItem>--SELECT--</asp:ListItem>

                       <asp:ListItem>MCA</asp:ListItem>

                       <asp:ListItem>BCA</asp:ListItem>

                       <asp:ListItem>MBA</asp:ListItem>

                       <asp:ListItem>BBA</asp:ListItem>

                       <asp:ListItem>BTech</asp:ListItem>

                       <asp:ListItem>MTech</asp:ListItem>

                   </asp:DropDownList>

               </td>

           </tr>

           <tr>

               <td class="style2">

                   &nbsp;</td>

               <td class="style3">

                   <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click"/>

                   <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Show"/>

               </td>

           </tr>

       </table>

       <asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"

BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both" RepeatDirection="Horizontal"

style="margin-right: 32px">

           <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510"/>

           <HeaderStyle BackColor="#A55129" Font-Bold="True"ForeColor="White"/>

           <ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510"/>

           <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White"/>

           <ItemTemplate>

           <hrsize=0 />

             Name:<%#DataBinder.Eval(Container.DataItem,"name")%><br/>

             Emp_id:<%#DataBinder.Eval(Container.DataItem,"Emp_id")%></a><br/>

             Qualification:<%#DataBinder.Eval(Container.DataItem,"Qualification")%><br/>

           </ItemTemplate>

       </asp:DataList>

   <br />

   </div>

   </form>

</body>

</html>

The design will look as in the following image:

 

designing.jpg
 

 

Step 2: Add a XML file by clicking "File" -> "Add new item" then click on "XML file". Name this XML file Sample.xml. Put the following code in the XML file:

 

<?xml version="1.0"encoding="utf-8"?>

<EmployeeInformation>

</EmployeeInformation>

 

Step 3: After that add the following namespaces in the code behind:

 

using System.Data;

using System.Xml;

Step 4: Now write the following code in the code behind file:

public partial classDefault2 : System.Web.UI.Page

{

 protected void Page_Load(object sender, EventArgs e)

    {

      if (!IsPostBack)

       {

           //Bind xml data to datalist

           BindDatalist();

       }

    }
  private void BindDatalist()

    {

       XmlTextReader xmlreader =new XmlTextReader(Server.MapPath("Employee.xml"));

       DataSet ds = newDataSet();

        ds.ReadXml(xmlreader);

        xmlreader.Close();

       if (ds.Tables.Count != 0)

        {

            DataList1.DataSource = ds;

            DataList1.DataBind();

        }

        else

        {

            DataList1.DataSource = null;

            DataList1.DataBind();

        }

    }

}

Step 5: Write the following code in the click event of the submit button, so that the data that is entered is stored in the XML File:
 

protected void Button1_Click(object sender, EventArgs e)

    {

       XmlDocument xmldoc =new XmlDocument();

        xmldoc.Load(Server.MapPath("Employee.xml"));

        BindDatalist();

    }

Step 6: Write the following code in the click event of the show button. Now the data stored in the XML file can be shown.

protected void Button2_Click(object sender, EventArgs e)

    {

       XmlDocument xmldoc =new XmlDocument();

        xmldoc.Load(Server.MapPath("Employee.xml"));

       XmlElement parentelement = xmldoc.CreateElement("Details");

       XmlElement name = xmldoc.CreateElement("Name");

        name.InnerText = TextBox1.Text;

       XmlElement Emp_id = xmldoc.CreateElement("Emp_id");

        Emp_id.InnerText = TextBox2.Text;

       XmlElement Qualification = xmldoc.CreateElement("Qualification");

        Qualification.InnerText = DropDownList1.SelectedItem.Text;

        parentelement.AppendChild(name);

        parentelement.AppendChild(Emp_id);

        parentelement.AppendChild(Qualification);

        xmldoc.DocumentElement.AppendChild(parentelement);

        xmldoc.Save(Server.MapPath("Employee.xml"));

        BindDatalist();

    }

Step 7: Run your application by pressing F5. The output is:

 

save.jpg
 

Step 8 : See your sample.xml file, it will include the data that I entered at the run time like this:

 

<?xmlversion="1.0"encoding="utf-8"?>

<EmployeeInformation>

  <Details>

    <Name>Richa</Name>

    <Emp_id>1</Emp_id>

    <Qualification>MCA</Qualification>

  </Details>

</EmployeeInformation>


Summary: In this article I use a XML file so that the data is stored and retrieved in XML using the Datalist in ASP.NET.


Similar Articles