Z ) XML DataBase

 XML DataBase


  •  XML :- Xtensionale Markup Languag
  • XML is markup language similar to Html , But without Predefined tag in use 
  • User Defined Html concept 
  • All tag is created by the User.
  • Using System.Data.XML;
  Q)   How to create XML
 Step :-  Create folder -- Select route -- C# -- XML File -- xml file name (Student.xml) --                      Browser -- select folder -- ok.

Q ) How create XML Database 
   * XML is reserve very less space 
      
 XMLFile Code :-  XMLsimple.xml

<guestbook>
  <entry name="ajay" city="pune">jhi</entry>
  <entry name="kanchan" city="bhopal">All The Best</entry>
  <entry name="raman" city="bhopal">save details</entry>
  <entry name="shubham" city="bhopal">save details</entry>
  <entry name="sourabh" city="bombay">save details</entry>
  <entry name="vijay" city="ahmdabad">i</entry>
  <entry name="naresh" city="kolhapur">am</entry>
  <entry name="suresh" city="shirdi">sourabh</entry>
  <entry name="sonu" city="shegaon">suresh</entry>
  <entry name="roni" city="nagpur">thakre</entry>
</guestbook>

Q ) How  to Display XML in Gridview 

 Source Code :-  XMLUploadAndGrid.aspx




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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <br />
        <br />
&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
        <br />
&nbsp;&nbsp; City&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
        <br />
        <br />
&nbsp;Message&nbsp;
        <asp:TextBox ID="txtmesage" runat="server"></asp:TextBox>
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" Height="27px" OnClick="Button2_Click" Text="Submit" Width="71px" />
        <br />
        <br />
    
        <br />
&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Height="32px" Text="Show" Width="94px" OnClick="Button1_Click" />
        <br />
        <br />
&nbsp;&nbsp;
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="281px" Width="382px">
            <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("city") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

                             Button Code :-  XMLUploadAndGrid.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;  // Xml library
using System.Data;

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

    protected void Button1_Click(object sender, EventArgs e)
    {

        XmlTextReader xmlread = new XmlTextReader(Server.MapPath("Z4_XMLSimple.xml"));

        DataSet ds = new DataSet();

        ds.ReadXml(xmlread);

        xmlread.Close();

        GridView1.DataSource = ds.Tables[0];

        GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        XmlDocument xmdoc = new XmlDocument();
        xmdoc.Load(Server.MapPath("Z4_XMLSimple.xml"));

        //read first tag in Xml 
        XmlNode xmnod = xmdoc.DocumentElement.FirstChild;

        //how to read element
        XmlElement xmele = xmdoc.CreateElement("entry");

        //how to set an element
        xmele.SetAttribute("name", Server.HtmlEncode(txtname.Text));
        xmele.SetAttribute("city", Server.HtmlEncode(txtcity.Text));

        //between the html tag <>inner text<>
        xmele.InnerText = Server.HtmlEncode(txtmesage.Text);

        // how to save record (InsertAfter/Insertbefore)
        xmdoc.DocumentElement.InsertAfter(xmele,xmnod);

        xmdoc.Save(Server.MapPath("Z4_XMLSimple.xml"));

        //save to all
    }
}



Comments

Popular posts from this blog

DATA CONTROL ( Gridview , Repeater , Formview , DataList , Detailsview , Listview )

Z 5 ) MVC (Model view Controller)

Z 8 ) MVC HTMLHELPER METHOD AND ACTIONLINK (how to use css ,atrributes,style in css)