Read Write XML File Data In Asp.Net C# VB

In this example i'm explaining how to Read And Write XML File Data In Asp.Net Using C# VB.NET. I have created a aspx page with three textbox to enter Details, Two buttons to read and write Data in click event of buttons.

I'm using Label control to display XML data after reading it.

Read write xml file in asp.net using c# vb.Net


HTML SOURCE OF PAGE
   1:  <div>
   2:  Employee Details:
   3:   
   4:  Name:
   5:  <asp:Textbox id="txtName" runat="server"/>
   6:   
   7:  Department:  
   8:  <asp:Textbox id="txtDept" runat="server"/>
   9:   
  10:  Location:  
  11:  <asp:Textbox id="txtLocation" runat="server"/>
  12:   
  13:  <asp:Button ID="btnWrite" runat="server" 
  14:              Text="Write XML File" 
  15:              onclick="btnWrite_Click"/>
  16:   
  17:  Read Data :
  18:  <asp:Button id="btnRead" text="Read XML File" 
  19:              runat="server" 
  20:              onclick="btnRead_Click"/>
  21:   
  22:  <asp:label id="lblMsg" runat="server"/>
  23:   
  24:  </div>

Write below mentioned code in click event of respective buttons.

C# CODE
using System;
using System.Xml;
using System.Text;

protected void btnRead_Click(object sender, EventArgs e)
    {
        ReadXmlFile(Server.MapPath("EmployeeDetails.xml"));
    }
    protected void btnWrite_Click(object sender, EventArgs e)
    {
        XmlTextWriter xWriter = new XmlTextWriter(Server.MapPath("EmployeeDetails.xml"), Encoding.UTF8);
        xWriter.WriteStartDocument();
        //Create Parent element
        xWriter.WriteStartElement("EmployeeDetails");
        //Create Child elements
        xWriter.WriteStartElement("Details");
        xWriter.WriteElementString("Name", txtName.Text);
        xWriter.WriteElementString("Department", txtDept.Text);
        xWriter.WriteElementString("Location", txtLocation.Text);
        xWriter.WriteEndElement();

        //End writing top element and XML document
        xWriter.WriteEndElement();
        xWriter.WriteEndDocument();
        xWriter.Close();
    }

    protected void ReadXmlFile(string fileName)
    {
        string parentElementName = "";
        string childElementName = "";
        string childElementValue = "";
        bool element = false;
        lblMsg.Text = "";
        
        XmlTextReader xReader = new XmlTextReader(fileName);
        while (xReader.Read())
        {
            if (xReader.NodeType == XmlNodeType.Element)
            {
                if (element)
                {
                   parentElementName = parentElementName + childElementName + "
"; } element = true; childElementName = xReader.Name; } else if (xReader.NodeType == XmlNodeType.Text | xReader.NodeType == XmlNodeType.CDATA) { element = false; childElementValue = xReader.Value; lblMsg.Text = lblMsg.Text + "" + parentElementName + "
" + childElementName + "

" + childElementValue; parentElementName = ""; childElementName = ""; } } xReader.Close(); }

VB.NET CODE
Protected Sub btnRead_Click(sender As Object, e As EventArgs)
 ReadXmlFile(Server.MapPath("EmployeeDetails.xml"))
End Sub
Protected Sub btnWrite_Click(sender As Object, e As EventArgs)
 Dim xWriter As New XmlTextWriter(Server.MapPath("EmployeeDetails.xml"), Encoding.UTF8)
 xWriter.WriteStartDocument()
 'Create Parent element
 xWriter.WriteStartElement("EmployeeDetails")
 'Create Child elements
 xWriter.WriteStartElement("Details")
 xWriter.WriteElementString("Name", txtName.Text)
 xWriter.WriteElementString("Department", txtDept.Text)
 xWriter.WriteElementString("Location", txtLocation.Text)
 xWriter.WriteEndElement()

 'End writing top element and XML document
 xWriter.WriteEndElement()
 xWriter.WriteEndDocument()
 xWriter.Close()
End Sub

Protected Sub ReadXmlFile(fileName As String)
 Dim parentElementName As String = ""
 Dim childElementName As String = ""
 Dim childElementValue As String = ""
 Dim element As Boolean = False
 lblMsg.Text = ""

 Dim xReader As New XmlTextReader(fileName)
 While xReader.Read()
  If xReader.NodeType = XmlNodeType.Element Then
   If element Then
    parentElementName = parentElementName & childElementName & "
" End If element = True childElementName = xReader.Name ElseIf xReader.NodeType = XmlNodeType.Text Or xReader.NodeType = XmlNodeType.CDATA Then element = False childElementValue = xReader.Value lblMsg.Text = lblMsg.Text + "" & parentElementName & "
" & childElementName & "

" & childElementValue parentElementName = "" childElementName = "" End If End While xReader.Close() End Sub


Download Sample Code



If you like this post than join us or share

0 comments:

Find More Articles