XMLRead.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.IO;

 

public partial class XMLRead : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            getContent();

        }

 

 

    }

    protected void btnsearch_Click(object sender, EventArgs e)

    {

 

        btnup.Visible = true;

        btndel.Visible = true;

        FileStream fs = new FileStream(@"C:\Documents and Settings\rajshree\Desktop\Library.xml", FileMode.Open, FileAccess.ReadWrite);

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(fs);

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + ddBookId.SelectedItem.Text + "']");

        if (nodeList.Count > 0)

        {

            txtid.Text = ddBookId.SelectedItem.Text;

            txtauthor.Text = nodeList[0].ChildNodes[0].InnerText;

            txttitle.Text = nodeList[0].ChildNodes[1].InnerText;

            txtgen.Text = nodeList[0].ChildNodes[2].InnerText;

            txtprice.Text = nodeList[0].ChildNodes[3].InnerText;

            txtpub.Text = nodeList[0].ChildNodes[4].InnerText;

            txtdesc.Text = nodeList[0].ChildNodes[5].InnerText;

        }

 

        fs.Close();

 

    }

    protected void getContent()

    {

 

        //XmlTextReader xr=new XmlTextReader(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        //while (xr.Read())

        //{

        //   if (xr.HasAttributes)

        //    {

        //          for (int i = 0; i < xr.AttributeCount; i++)

        //        {

 

        //            ddBookId.Items.Add(xr[i]);

        //        }

 

        //        ddBookId.DataBind();

        //    }

        //}

        //xr.Close();

 

        XmlDocument doc = new XmlDocument();

        doc.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc.SelectNodes("/catalog/book");

 

        foreach (XmlElement book in doc.SelectNodes(@"catalog/book"))

        {

            ddBookId.Items.Add(book.GetAttribute("id"));

        }

 

    }

 

    protected void btnadd_Click(object sender, EventArgs e)

    {

 

 

        XmlDocument doc = new XmlDocument();

 

        doc.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

 

        XmlElement root = doc.CreateElement("book");

        root.SetAttribute("id", txtid.Text);

        XmlElement title = doc.CreateElement("title");

        XmlElement author = doc.CreateElement("author");

        XmlElement genre = doc.CreateElement("genre");

        XmlElement price = doc.CreateElement("price");

        XmlElement publish_date = doc.CreateElement("publish_date");

        XmlElement description = doc.CreateElement("description");

 

        title.InnerText = txttitle.Text;

        author.InnerText = txtauthor.Text;

        genre.InnerText = txtgen.Text;

        price.InnerText = txtprice.Text;

        publish_date.InnerText = txtpub.Text;

        description.InnerText = txtdesc.Text; ;

 

        root.AppendChild(author);

        root.AppendChild(title);

        root.AppendChild(genre);

        root.AppendChild(price);

        root.AppendChild(publish_date);

        root.AppendChild(description);

 

        doc.DocumentElement.AppendChild(root);

        doc.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

 

        lblerror.Text = "Element has been added successfully";

        lblerror.Visible = true;

        txtauthor.Text = "";

        txttitle.Text = "";

        txtdesc.Text = "";

        txtgen.Text = "";

        txtid.Text = "";

        txtprice.Text = "";

        txtpub.Text = "";

    }

 

    protected void btnup_Click(object sender, EventArgs e)

    {

 

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + ddBookId.SelectedItem.Text + "']");

        nodeList[0].ChildNodes[4].InnerText = txtpub.Text;

 

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        lblerror.Visible = true;

        lblerror.Text = "Updated Successfully";

        txtauthor.Text = "";

        txttitle.Text = "";

        txtdesc.Text = "";

        txtgen.Text = "";

        txtid.Text = "";

        txtprice.Text = "";

        txtpub.Text = "";

 

 

    }

    protected void btndel_Click(object sender, EventArgs e)

    {

        XmlDocument doc2 = new XmlDocument();

        doc2.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNode nodeList = doc2.SelectSingleNode("/catalog/book[@id='" + ddBookId.SelectedItem.Text + "']");

        doc2.DocumentElement.RemoveChild(nodeList);

        doc2.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        lblerror.Text = "Deleted Successfully";

        lblerror.Visible = true;

        txtauthor.Text = "";

        txttitle.Text = "";

        txtdesc.Text = "";

        txtgen.Text = "";

        txtid.Text = "";

        txtprice.Text = "";

        txtpub.Text = ""
 
    }
}

ExWithGrid.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;

 

public partial class ExWithGrid : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            getdata();

        }

    }

    private void getdata()

    {

        DataSet ds = new DataSet();

        ds.ReadXml(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        GridView1.DataSource = ds;

        GridView1.DataBind();

    }

 

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

        string id = row.Cells[1].Text.Trim();

 

        XmlDocument doc2 = new XmlDocument();

        doc2.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNode nodeList = doc2.SelectSingleNode("/catalog/book[@id='" + id + "']");

        doc2.DocumentElement.RemoveChild(nodeList);

        doc2.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        getdata();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        GridView1.EditIndex = e.NewEditIndex;

        getdata();

    }

 

    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)

    {

        getdata();

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

        string pub = row.Cells[6].Text.Trim();

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + row.Cells[1].Text + "']");

        nodeList[0].ChildNodes[4].InnerText = pub;

 

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        getdata();

    }

    protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)

    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

        string pub = (row.Cells[6].Controls[0] as TextBox).Text;

        string a = row.Cells[2].Text;

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        XmlNodeList nodeList = doc1.SelectNodes("/catalog/book[@id='" + row.Cells[1].Text + "']");

        nodeList[0].ChildNodes[4].InnerText = pub;

        doc1.Save(@"C:\Documents and Settings\rajshree\Desktop\Library.xml");

        getdata();

    }

    protected void GridView1_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e)

    {

        GridView1.EditIndex = -1;

        getdata();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        GridView1.PageIndex = e.NewPageIndex;

        getdata();

 

    }

}

Next Recommended Readings