WE Require 2 Files Default.aspx And Students.XML. Write Following Code In Default.aspx and just add Students.XML (Do Not Create Any Tag Manually In XML File)



DEFAULT.ASPX

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table border="1">
        <tr>
            <td>Student</td>
            <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Name</td>
            <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Address</td>
            <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td colspan="2" align="Center"><asp:Button ID="Button1" runat="server" Text="Write XML" OnClick="Button1_Click" /></td>

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



DEFAULT.ASPX.CS

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Server.MapPath("Students.xml"));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
            DataSet ds = CreateDynamicDataSet();
            ds.WriteXml(Server.MapPath("Students.xml"));
    }
    private DataSet CreateDynamicDataSet()
    {

        DataSet ds = new DataSet("DS");
        ds.Namespace = "StdNamespace";
        DataTable stdTable = new DataTable("Student");
        DataColumn col1 = new DataColumn("Name");
        DataColumn col2 = new DataColumn("Address");
        stdTable.Columns.Add(col1);
        stdTable.Columns.Add(col2);
        ds.Tables.Add(stdTable);

        //Add student Data to the table
        DataRow newRow; newRow = stdTable.NewRow();
        newRow["Name"] = TextBox2.Text;
        newRow["Address"] = TextBox3.Text;
        stdTable.Rows.Add(newRow);
        newRow = stdTable.NewRow();

        return ds;
    }
}
Next
This is the most recent post.
Previous
Older Post

Post a Comment

 
Top