blogspot visit counter

Friday, 14 June 2013

crete and read xml file in asp.net

crete and read xml file in asp.net
Introduction :-

In this article i am explain how i can create a xml file dynamically in asp.net  .

Description :-

In my previous article i have explained how i can read the XML file in asp.net.Know i will explain how i can write data in XML file and read from the XML file which data i have inserted .

Note :- You have no need to create a xml file manually at the running time automatically create the XML file .

 Aspx Design code as shown below



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

<!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>Read and write into xml file</title>


    <style type="text/css">
        .style1
        {
            text-align: left;
        }
        .style2
        {
            color: #FFFFFF;
            background-color:Olive;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
    <b style="text-align: left"><span class="style2">Read Write XML File in Asp.Net<br />
        <br />
        <br />
        </span></b><br /></div>
    <table width="50%"><tr><td width="50%" valign="top">
<b>Employee Details:</b><br /><br />

<table>
<tr><td>Name:</td>
<td><asp:Textbox id="txtName" runat="server"/></td></tr>

<tr><td>Department: </td>
<td><asp:Textbox id="txtDept" runat="server"/></td></tr>

<tr><td>Location: </td>
<td><asp:Textbox id="txtLocation" runat="server"/></td></tr>

<tr><td colspan="2" align="right">
<asp:Button ID="btnWriteXml" runat="server" Text="Write XML File"
        onclick="btnWriteXml_Click" style="height: 26px"/>
</td></tr></table></td>

<td width="50%" valign="top">Read XML File :
<asp:Button id="btnReadXml" text="Read XML File" runat="server"
        onclick="btnReadXml_Click"/>
<br/>
<asp:label id="lblXml" runat="server"/></td></tr>

</table>
    </div>
    </center>
    </form>
</body>
</html>


 Include the name space in code behind file for xml read and writer and Encoding.UTF8
as shown below


using System.Xml; //for xml
using System.Text; //Encoding.UTF8


Code behind  file code as shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml; //for xml
using System.Text; //Encoding.UTF8

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

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

        //End writing top element and XML document
        xmlWriter.WriteEndElement();
        xmlWriter.WriteEndDocument();
        xmlWriter.Close();
        txtDept.Text = "";
        txtLocation.Text = "";
        txtName.Text = "";

    }
    protected void btnReadXml_Click(object sender, EventArgs e)
    {
        ReadXmlFile(Server.MapPath("EmployeeDetails.xml"));
    }
    protected void ReadXmlFile(string xmlFile)
    {
        string parentElementName = "";
        string childElementName = "";
        string childElementValue = "";
        bool element = false;
        lblXml.Text = "";

        XmlTextReader xmlReader = new XmlTextReader(xmlFile);
        while (xmlReader.Read())
        {
            if (xmlReader.NodeType == XmlNodeType.Element)
            {
                if (element)
                {
                    parentElementName = parentElementName + childElementName + "<br/>";
                }
                element = true;
                childElementName = xmlReader.Name;
            }
            else if (xmlReader.NodeType == XmlNodeType.Text | xmlReader.NodeType == XmlNodeType.CDATA)
            {
                element = false;
                childElementValue = xmlReader.Value;
                lblXml.Text = lblXml.Text + "<b>" + parentElementName + "<br/>" + childElementName + "</b><br/>" + childElementValue;
                parentElementName = "";
                childElementName = "";
            }
        }
        xmlReader.Close();
    }
}

 Output as shown below

After Insert the Record into the textbox and press the button Write XML File  And Click on Read XML File  as shown below




Download sample code attached



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...