Showing posts with label Inserted data to Creating Xml Tag. Show all posts
Showing posts with label Inserted data to Creating Xml Tag. Show all posts

Monday, July 14, 2014

Inserted data to Creating Xml Tag

Add Xml File And Create Root Tag like <bookstore>  </bookstore>
Xml Name like BookStore.xml

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <table style="width: 100%;">
            <tr>
                <td class="auto-style1">
                    Title:
                </td>
                <td>
                    <asp:TextBox ID="tbTitle" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Author:
                </td>
                <td>
                    <asp:TextBox ID="tbAuthor" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Year:
                </td>
                <td>
                    <asp:TextBox ID="tbYear" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                    Price:
                </td>
                <td>
                    <asp:TextBox ID="tbPrice" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style1">
                </td>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
        <asp:GridView ID="gvBookStoreRecords" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

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.Xml;
using System.Data;
using System.Data.SqlClient;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        XmlDocument XmlDocObj = new XmlDocument();
        XmlDocObj.Load(Server.MapPath("BookStore.xml"));
        XmlNode RootNode = XmlDocObj.SelectSingleNode("bookstore");
        XmlNode bookNode = RootNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "book", ""));
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Title", "")).InnerText = tbTitle.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Author", "")).InnerText = tbAuthor.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Year", "")).InnerText = tbYear.Text;
        bookNode.AppendChild(XmlDocObj.CreateNode(XmlNodeType.Element, "Price", "")).InnerText = tbPrice.Text;

        XmlDocObj.Save(Server.MapPath("BookStore.xml"));

        gridDataBind();

    }
    public void gridDataBind()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("BookStore.xml"));
        gvBookStoreRecords.DataSource = ds;
        gvBookStoreRecords.DataBind();
    }
}