Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Tuesday, 30 June 2015

In this article I will explain how to write data to xml file and read from it in asp.Net using c#.

Steps to follow to write and read data from XML file:

1.      First of all create a website and then create on xml File ex. XMLFile.xml.
2.      Then create one webpage and design this webpage to insert data into xml file and show data in Datalist control as given below:

Design Section:

Create your webpage as given below:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <fieldset style="width:400px;">
    <legend>Read And Write In Xml File In asp.net </legend>
   
        <table class="style1">
            <tr>
                <td>
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Age</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Department</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Salary</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:DataList ID="DataList1" runat="server">
                    <ItemTemplate>
                    Name:<%#Eval("name")%><br />
                    Age:<%#Eval("Age")%><br />
                    Department:<%#Eval("Department")%><br />
                    Salary:<%#Eval("Salary")%><br /><br />
                   
                    </ItemTemplate>
                    </asp:DataList>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </fieldset>
    </div>
    </form>
</body>
</html>



ASP.NET Code using C#:

After designing the webpage, write the following code in code behind file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class XML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("XMLFile.xml"));
        XmlElement Testing = xmldoc.CreateElement("Testing");
        XmlElement name = xmldoc.CreateElement("Name");
        name.InnerText = TextBox1.Text;
        XmlElement age = xmldoc.CreateElement("Age");
        age.InnerText = TextBox2.Text;
        XmlElement department = xmldoc.CreateElement("department");
        department.InnerText = TextBox3.Text;
        XmlElement salary = xmldoc.CreateElement("salary");
        salary.InnerText = TextBox4.Text;
        Testing.AppendChild(name);
        Testing.AppendChild(age);
        Testing.AppendChild(department);
        Testing.AppendChild(salary);
        xmldoc.DocumentElement.AppendChild(Testing);
        xmldoc.Save(Server.MapPath("XMLFile.xml"));
        bindlist();
    }
    public void bindlist()
    {
        XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("XMLFile.xml"));
        DataSet ds = new DataSet();
        ds.ReadXml(xmlreader);
        xmlreader.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataList1.DataSource = ds;
            DataList1.DataBind();
        }
   
    }
}

0 comments:

Post a Comment