Ads 468x60px

For New Update Use this Link Offer for you


Friday, 14 June 2013

crete and read xml file in

crete and read xml file in
Introduction :-

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

Description :-

In my previous article i have explained how i can read the XML file in 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" "">

<html xmlns="">
<head runat="server">
    <title>Read and write into xml file</title>

    <style type="text/css">
            text-align: left;
            color: #FFFFFF;
    <form id="form1" runat="server">
    <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 />

<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 width="50%" valign="top">Read XML File :
<asp:Button id="btnReadXml" text="Read XML File" runat="server"
<asp:label id="lblXml" runat="server"/></td></tr>


 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);
        //Create Parent element
        //Create Child elements
        xmlWriter.WriteElementString("Name", txtName.Text);
        xmlWriter.WriteElementString("Department", txtDept.Text);
        xmlWriter.WriteElementString("Location", txtLocation.Text);

        //End writing top element and XML document
        txtDept.Text = "";
        txtLocation.Text = "";
        txtName.Text = "";

    protected void btnReadXml_Click(object sender, EventArgs e)
    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 = "";

 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



New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result