XML stands for Extensible markup language. By using XML we can easily retrieve and store data and display the data without using databases in our applications. If we need to display dynamic data in our application it will take time to connect databases and retrieve data from databases but if we use XML to store data we can do operations with xml files directly without using databases. If we store data in database that is incompatible to some of the computer applications but if we store data in XML format it will support all applications. It is independent for all software applications and it is accessible with all applications.
How to create new Xml file.
Step-
- First we use namespace System.Xml;
- XmlDocument xmlDoc = new XmlDocument();
- Create root Node-
- XmlNode rootNode = xmlDoc.CreateElement("Contact");
- Create Sub node-
- XmlNode subNode = xmlDoc.CreateElement("Detail");
- Append root node and sub node-
- xmlDoc.AppendChild(rootNode);
- rootNode.AppendChild(subNode);
- XmlNode userNode = xmlDoc.CreateElement("Name");
- userNode.InnerText = txtName1.Text;
- subNode.AppendChild(userNode);
- xmlDoc.Save(HttpContext.Current.Server.MapPath("~/Upload") + "/" + "ContactPerson.xml");
How to read Xml file
- Namespace for dataset System.Data
- XmlTextReader object use for assign path of physical xml File.
- DataSet ds = new DataSet();
- ds.ReadXml(xmlreader);
- xmlreader.Close();
- Open Visual Studio.
- Click File menu -> click new Website(Name XMlTest)
- Go to solution explorer create XMlDemo.aspx page
Source Code(XMlDemo.aspx)
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="XMlDemo.aspx.cs" Inherits="XMlDemo" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td style="width: 100px">
- Name:</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtName1" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="width: 100px">
- Address:</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtLocation1" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="width: 100px">
- Email:</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtEmail1" runat="server"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td style="width: 100px" valign="top">
- Phone:</td>
- <td style="width: 100px">
- <asp:TextBox ID="txtPhone1" runat="server" TextMode="MultiLine" Height="104px"></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
- </td>
- </tr>
- </table>
- <br />
- <asp:DataList ID="dlComments" Runat="server" Width="100%">
- <ItemTemplate>
- <hr size=0/> Name:
- <%# DataBinder.Eval(Container.DataItem, "name") %><br /> E-mail:
- <a href="mailto:<%# DataBinder.Eval(Container.DataItem, " email ") %>">
- <%# DataBinder.Eval(Container.DataItem, "email") %>
- </a><br /> Location:
- <%# DataBinder.Eval(Container.DataItem, "Address") %><br /> Email:
- <%# DataBinder.Eval(Container.DataItem, "EMail") %><br /> Phone:
- <%# DataBinder.Eval(Container.DataItem, "Phone") %>
- </ItemTemplate>
- </asp:DataList>
- </div>
- </form>
- </body>
-
- </html>
Code behind page(XMlDemo.aspx.cs)