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-

How to read Xml file

Source Code(XMlDemo.aspx)

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="XMlDemo.aspx.cs" Inherits="XMlDemo" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <table>
  11. <tr>
  12. <td style="width: 100px">
  13. Name:</td>
  14. <td style="width: 100px">
  15. <asp:TextBox ID="txtName1" runat="server"></asp:TextBox>
  16. </td>
  17. </tr>
  18. <tr>
  19. <td style="width: 100px">
  20. Address:</td>
  21. <td style="width: 100px">
  22. <asp:TextBox ID="txtLocation1" runat="server"></asp:TextBox>
  23. </td>
  24. </tr>
  25. <tr>
  26. <td style="width: 100px">
  27. Email:</td>
  28. <td style="width: 100px">
  29. <asp:TextBox ID="txtEmail1" runat="server"></asp:TextBox>
  30. </td>
  31. </tr>
  32. <tr>
  33. <td style="width: 100px" valign="top">
  34. Phone:</td>
  35. <td style="width: 100px">
  36. <asp:TextBox ID="txtPhone1" runat="server" TextMode="MultiLine" Height="104px"></asp:TextBox>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td></td>
  41. <td>
  42. <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
  43. </td>
  44. </tr>
  45. </table>
  46. <br />
  47. <asp:DataList ID="dlComments" Runat="server" Width="100%">
  48. <ItemTemplate>
  49. <hr size=0/> Name:
  50. <%# DataBinder.Eval(Container.DataItem, "name") %><br /> E-mail:
  51. <a href="mailto:<%# DataBinder.Eval(Container.DataItem, " email ") %>">
  52. <%# DataBinder.Eval(Container.DataItem, "email") %>
  53. </a><br /> Location:
  54. <%# DataBinder.Eval(Container.DataItem, "Address") %><br /> Email:
  55. <%# DataBinder.Eval(Container.DataItem, "EMail") %><br /> Phone:
  56. <%# DataBinder.Eval(Container.DataItem, "Phone") %>
  57. </ItemTemplate>
  58. </asp:DataList>
  59. </div>
  60. </form>
  61. </body>
  62. </html>
Code behind page(XMlDemo.aspx.cs)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Xml;
  11. using System.IO;
  12. public partial class XMlDemo: System.Web.UI.Page
  13. {
  14. public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
  15. protected void Page_Load(object sender, EventArgs e)
  16. {
  17. if (!IsPostBack)
  18. {
  19. if (Directory.Exists(HttpContext.Current.Server.MapPath("~/Upload")))
  20. BindDatalist();
  21. }
  22. }
  23. protected void btnSubmit_Click(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. if (txtName1.Text != "" && txtEmail1.Text != "" && txtPhone1.Text != "" && txtLocation1.Text != "")
  28. {
  29. if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/Upload"))) // if not created then it will create it.
  30. {
  31. Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/Upload"));
  32. XmlDocument xmlDoc = new XmlDocument();
  33. XmlNode rootNode = xmlDoc.CreateElement("Contact");
  34. XmlNode subNode = xmlDoc.CreateElement("Detail");
  35. xmlDoc.AppendChild(rootNode);
  36. rootNode.AppendChild(subNode);
  37. XmlNode userNode = xmlDoc.CreateElement("Name");
  38. userNode.InnerText = txtName1.Text;
  39. subNode.AppendChild(userNode);
  40. XmlNode userEmail = xmlDoc.CreateElement("Email");
  41. userEmail.InnerText = txtEmail1.Text;
  42. subNode.AppendChild(userEmail);
  43. XmlNode userPhone = xmlDoc.CreateElement("Phone");
  44. userPhone.InnerText = txtPhone1.Text;
  45. subNode.AppendChild(userPhone);
  46. XmlNode userAddress = xmlDoc.CreateElement("Address");
  47. userAddress.InnerText = txtLocation1.Text;
  48. subNode.AppendChild(userAddress);
  49. xmlDoc.Save(HttpContext.Current.Server.MapPath("~/Upload") + "/" + "ContactPerson.xml");
  50. BindDatalist();
  51. }
  52. else
  53. {
  54. string FileName = HttpContext.Current.Server.MapPath("~/Upload") + "/" + "ContactPerson.xml";
  55. //IDVal=XDocument.Load(HttpContext.Current.Server.MapPath("~/ContactFile") + "/" + "ContactPerson.xml");
  56. // int Id = (int)(from b in Contact.Descendants ("Detail") orderby (int)b.Element("ID") descending select (int)b.Element("ID")).FirstOrDefault() + 1;
  57. XmlDocument xmlDoc = new XmlDocument();
  58. xmlDoc.Load(FileName);
  59. XmlNode Root = xmlDoc.DocumentElement;
  60. System.Xml.XmlNode myXmlNode = xmlDoc.DocumentElement.FirstChild;
  61. XmlElement page = xmlDoc.CreateElement("Detail");
  62. XmlElement Name = xmlDoc.CreateElement("Name");
  63. XmlElement Email = xmlDoc.CreateElement("Email");
  64. XmlElement Phone = xmlDoc.CreateElement("Phone");
  65. XmlElement Address = xmlDoc.CreateElement("Address");
  66. XmlText textName = xmlDoc.CreateTextNode(txtName1.Text);
  67. XmlText txtEmail = xmlDoc.CreateTextNode(txtEmail1.Text);
  68. XmlText txtPhone = xmlDoc.CreateTextNode(txtPhone1.Text);
  69. XmlText txtAddress = xmlDoc.CreateTextNode(txtLocation1.Text);
  70. page.AppendChild(Name);
  71. page.AppendChild(Email);
  72. page.AppendChild(Phone);
  73. page.AppendChild(Address);
  74. Name.AppendChild(textName);
  75. Email.AppendChild(txtEmail);
  76. Phone.AppendChild(txtPhone);
  77. Address.AppendChild(txtAddress);
  78. xmlDoc.DocumentElement.InsertBefore(page, myXmlNode);
  79. xmlDoc.Save(FileName);
  80. BindDatalist();
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {}
  86. finally
  87. {
  88. txtLocation1.Text = "";
  89. txtPhone1.Text = "";
  90. txtEmail1.Text = "";
  91. txtName1.Text = "";
  92. }
  93. }
  94. private void BindDatalist()
  95. {
  96. XmlTextReader xmlreader = new XmlTextReader(HttpContext.Current.Server.MapPath("~/Upload") + "/" + "ContactPerson.xml");
  97. DataSet ds = new DataSet();
  98. ds.ReadXml(xmlreader);
  99. xmlreader.Close();
  100. if (ds.Tables.Count != 0)
  101. {
  102. dlComments.DataSource = ds;
  103. dlComments.DataBind();
  104. }
  105. else
  106. {
  107. dlComments.DataSource = null;
  108. dlComments.DataBind();
  109. }
  110. }
  111. }

Output

Run