Read Write XML Data-Read Write XML File Using C#, VB.NET In Asp.Net

Read Write XML File Using C#, VB.NET In Asp.Net 
In this post i am going to tell how you can read and write  XML data from XML file in asp.net using C# .

For Creating this example i have created one aspx page which contains three textbox to enter Customer name, age and location.

And there are two button control one is to write data to xml file and another button to read data from xml file .after reading data it will showed in a label control.

Aspx page code is here 


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadWriteXmlData.aspx.cs" Inherits="ReadWriteXmlData" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1 px solid Red" >
    <tr><td colspan="2">Customer Details</td></tr>
    <tr><td >Name:<asp:TextBox ID="txtname" runat="server" Width="200px"></asp:TextBox></td></tr>
    <tr><td >Age:<asp:TextBox ID="txtage" runat="server" Width="200px"></asp:TextBox></td></tr>
    <tr><td >Location:<asp:TextBox ID="txtlocation" runat="server" Width="200px"></asp:TextBox></td></tr>
    <tr><td ><asp:Button ID="btnwrite" runat="server" OnClick="btnwrite_click" Text="Write To Xml" /><asp:Button Text="Read From Xml" ID="btnread" OnClick="btnread_click" runat="server" /></td></tr>
    <tr><td><asp:label id="lblXmlData" runat="server"/></td></tr>
    </table>
    </div>
    </form>
</body>
</html>

CS  Page Code 


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;

public partial class ReadWriteXmlData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnwrite_click(object o, EventArgs e)
    {
        XmlTextWriter MyWriter = new XmlTextWriter(Server.MapPath("Details.xml"), Encoding.UTF8);
        MyWriter.WriteStartDocument();
        //Create root element Node
        MyWriter.WriteStartElement("CustDetails");
        //Create Child elements
        MyWriter.WriteStartElement("Details");
        MyWriter.WriteElementString("Name", txtname.Text);
        MyWriter.WriteElementString("Age", txtage.Text);
        MyWriter.WriteElementString("Location", txtlocation.Text);
        MyWriter.WriteEndElement();
        //now end top element
        MyWriter.WriteEndElement();
        MyWriter.WriteEndDocument();
        MyWriter.Close();

    }

    protected void btnread_click(object o, EventArgs e)
    {
        string rootElementName = "";
        string childElementName = "";
        string childElementValue = "";
        bool element = false;
        lblXmlData.Text = "";
        XmlTextReader MyxmlReader = new XmlTextReader(Server.MapPath("Details.xml"));
        while (MyxmlReader.Read())
        {
            if (MyxmlReader.NodeType == XmlNodeType.Element)
          {
                if (element)
               {
                   rootElementName = rootElementName + "<br/>" + childElementName + "<br/>";
               }
                element = true;
                childElementName = MyxmlReader.Name;
            }
           else if (MyxmlReader.NodeType == XmlNodeType.Text | MyxmlReader.NodeType == XmlNodeType.CDATA)
            {
                element = false;
                childElementValue = MyxmlReader.Value;
                lblXmlData.Text = lblXmlData.Text + "<b>" + rootElementName + "<br/>" + childElementName + "<br/></b>" + childElementValue;
                rootElementName = "";
                childElementName = "";
            }
        }
        MyxmlReader.Close();
        }
    }
 
Tags: , , ,

Join Us!