XML with ASP.NET

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;
    1. XmlDocument xmlDoc = new XmlDocument();  
  • Create root Node-
    1. XmlNode rootNode = xmlDoc.CreateElement("Contact");  
  • Create Sub node-
    1. XmlNode subNode = xmlDoc.CreateElement("Detail");  
  • Append root node and sub node-
    1. xmlDoc.AppendChild(rootNode);  
    2. rootNode.AppendChild(subNode);  
    3. XmlNode userNode = xmlDoc.CreateElement("Name");  
    4. userNode.InnerText = txtName1.Text;  
    5. subNode.AppendChild(userNode);  
    6. 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.
    1. DataSet ds = new DataSet();  
    2. ds.ReadXml(xmlreader);  
    3. 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)

  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.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <table>  
  13.                     <tr>  
  14.                         <td style="width: 100px">  
  15.                             Name:</td>  
  16.                         <td style="width: 100px">  
  17.                             <asp:TextBox ID="txtName1" runat="server"></asp:TextBox>  
  18.                         </td>  
  19.                     </tr>  
  20.                     <tr>  
  21.                         <td style="width: 100px">  
  22.                             Address:</td>  
  23.                         <td style="width: 100px">  
  24.                             <asp:TextBox ID="txtLocation1" runat="server"></asp:TextBox>  
  25.                         </td>  
  26.                     </tr>  
  27.                     <tr>  
  28.                         <td style="width: 100px">  
  29.                             Email:</td>  
  30.                         <td style="width: 100px">  
  31.                             <asp:TextBox ID="txtEmail1" runat="server"></asp:TextBox>  
  32.                         </td>  
  33.                     </tr>  
  34.                     <tr>  
  35.                         <td style="width: 100px" valign="top">  
  36.                             Phone:</td>  
  37.                         <td style="width: 100px">  
  38.                             <asp:TextBox ID="txtPhone1" runat="server" TextMode="MultiLine" Height="104px"></asp:TextBox>  
  39.                         </td>  
  40.                     </tr>  
  41.                     <tr>  
  42.                         <td></td>  
  43.                         <td>  
  44.                             <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />  
  45.                         </td>  
  46.                     </tr>  
  47.                 </table>  
  48.                 <br />  
  49.                 <asp:DataList ID="dlComments" Runat="server" Width="100%">  
  50.                     <ItemTemplate>  
  51.                         <hr size=0/> Name:  
  52.                         <%# DataBinder.Eval(Container.DataItem, "name") %><br /> E-mail:  
  53.                             <a href="mailto:<%# DataBinder.Eval(Container.DataItem, " email ") %>">  
  54.                                 <%# DataBinder.Eval(Container.DataItem, "email") %>  
  55.                             </a><br /> Location:  
  56.                             <%# DataBinder.Eval(Container.DataItem, "Address") %><br /> Email:  
  57.                                 <%# DataBinder.Eval(Container.DataItem, "EMail") %><br /> Phone:  
  58.                                     <%# DataBinder.Eval(Container.DataItem, "Phone") %>  
  59.                     </ItemTemplate>  
  60.                 </asp:DataList>  
  61.             </div>  
  62.         </form>  
  63.     </body>  
  64.   
  65. </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

Ebook Download
View all
Learn
View all