Sunday, 1 April 2018

How To Insert Data into XML File Using Asp.Net

Dear all In this article emphasize the essence of XML file and elaborated how to insert the data using XML file with Asp.Net.Let's have a Deep dive of this implementation.



Let me Brief What is XML ?

  • Acronym for XML is Extensible Mark Up Language
  • It facilitates an Standrad Way of communication in between two incompitable systems.
  • XML is used to  Transfer the Data rather than display of Data .
  • Which is an Mark Up Langauge Like HTML.But There is no relatation in between HTML and XML
  • XML contains a tags every tag must have an close tags.These tags are not predefined.We must define our own tags
  • XML tags are self descriptive.Which means we have to describe the tags
  • XML will be supported W3C Consortium 
  • Every xml file has be extended with .xml

Let's See Structure of an Typical XML File


<xml version="1.0">

<EmployeeList>-->Root Tag

<Emp Name="abc" Designation="Software Engineer" Qualification ="B.Tech"></Emp>-->Parent Tag

<Emp Name="lmn" Designation="Software Engineer" Qualification ="BCA"></Emp>
<Emp Name="pqr" Designation="Software Engineer" Qualification ="M.Tech"></Emp>
<Emp Name="xyz" Designation="Software Engineer" Qualification ="MCA"></Emp>
</EmployeeList>

Steps to Work with XML
  • Go To Visual Studio Click on New-->Project-->Select Empty Website
  • Name the Application as Xml Demo and click on Ok
  • Go to Solution Explorer and Right Click on the Project -->Clik on Add New -->Folder Named it as Files
  • Now Design the Form With the Following Controls
  • Right Click on the Project -->Click on Add New Item -->Select Web Form Name it as InsertEmployee
  • Design the Form by Placing the following Code in InsertEmployee.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td>Enter Name</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter Qualification</td>
                <td>
                    <asp:TextBox ID="txtQualification" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter Designation</td>
                <td>
                    <asp:TextBox ID="txtDesignation" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Write the Following Code in Button Click


Use the Following Namespaces

using System.Xml;
using System.IO;

Code :

string Path = string.Empty;
            Path = Server.MapPath("~/Files/") + "Emp.xml";
            XmlDocument doc = new XmlDocument();
            if (!File.Exists(Path))
            {
                XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlElement root = doc.CreateElement("EmployeeList");
                XmlElement parent = doc.CreateElement("Employee");
                XmlAttribute Name = doc.CreateAttribute("Name");
                XmlAttribute Qualification = doc.CreateAttribute("Qualification");
                XmlAttribute Designation = doc.CreateAttribute("Designation");

                Name.Value = txtName.Text;
                Qualification.Value = txtQualification.Text;
                Designation.Value = txtDesignation.Text;

                doc.AppendChild(declaration);
                doc.AppendChild(root);
                root.AppendChild(parent);

                parent.Attributes.Append(Name);
                parent.Attributes.Append(Qualification);
                parent.Attributes.Append(Designation);
                doc.Save(Path);
            }
            else
            {
                doc.Load(Path);
                XmlElement root = doc.DocumentElement;
                XmlDocument doc1 = new XmlDocument();
                doc1.Load(Path);

                XmlElement parent = doc.CreateElement("Employee");
                XmlAttribute Name = doc.CreateAttribute("Name");
                XmlAttribute Qualification = doc.CreateAttribute("Qualification");
                XmlAttribute Designation = doc.CreateAttribute("Designation");

                Name.Value = txtName.Text;
                Qualification.Value = txtQualification.Text;
                Designation.Value = txtDesignation.Text;

                parent.Attributes.Append(Name);
                parent.Attributes.Append(Qualification);
                parent.Attributes.Append(Designation);

                root.AppendChild(parent);
                doc.Save(Path);
            }

            Response.Write("<script>alert('Data Inserted Sucessfully')</script>");
        


No comments :

Post a Comment