CRUD Operation Using ListView With XML Database

Posted by Raj Kumar Articles | XML October 11, 2010
This article describes how to use CRUD (Create, Read, Update, Delete) Operation using ListView using XML database in ASP.NET.
Reader Level:
Download Files:
 


This article describes how to use CRUD (Create, Read, Update, Delete) Operation using ListView using XML database in ASP.NET.

First of all add a new XML file using add new item...here is a XML file I took from an article in www.c-sharpcorner.com.

MovieDB.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Movies
  <
Movie>
    <
Name>Scanner Darklya</Name>
    <Year>2009</Year>
    <Genre>Drama, Crime, Animation</Genre>
    <Cast>Keanu Reeves</Cast>
  </Movie>
  <
Movie>
    <
Name>Afro Samurai: Resurrection</Name>
    <Year>2009</Year>
    <Genre>Animation, Action</Genre>
    <Cast>No Cast</Cast>
  </Movie>
  <
Movie>
    <
Name>Batman: Gotham Knight</Name>
    <Year>2009</Year>
    <Genre>Animation, Action</Genre>
    <Cast>Batman</Cast>
  </Movie>
  <
Movie>
    <
Name>Bedazzled</Name>
    <Year>2009</Year>
    <Genre>Thriller, Comedy</Genre>
    <Cast>Brendan Fraser, Elizabeth Hurley</Cast>
  </Movie>
  <
Movie>
    <
Name>Bedtime Stories</Name>
    <Year>2009</Year>
    <Genre>Drama, Comedy</Genre>
    <Cast>Adam Sanders, Courtney Cox</Cast>
  </Movie>
</Movies>

Now create a new asp.net application and drag and drop a ListView control from toolbox.

<asp:ListView ID="MoviesListView" runat="server"
        InsertItemPosition="LastItem" OnItemInserting="MoviesListView_ItemInserting"
        OnItemDeleting="MoviesListView_ItemDeleting" OnItemEditing="MoviesListView_ItemEditing"
        OnItemCanceling="MoviesListView_ItemCanceling" >
        <LayoutTemplate>
        <table id="Table1" runat="server" width=100%>
            <tr id="Tr1" runat="server">
            <td id="Td1" runat="server">
            <table id="itemPlaceholderContainer" runat="server" border="0" style="" width=100%>
            <tr id="Tr2" runat="server" style="">
            <th id="Th1" runat="server"></th>
            <th id="Th2" runat="server">Name</th>
            <th id="Th3" runat="server">Year</th>
            <th id="Th4" runat="server">Genre</th>
            <th id="Th5" runat="server">Cast</th>
            </tr>
            <tr id="itemPlaceholder" runat="server"></tr></table></td></tr>
            <tr id="Tr3" runat="server">
            <td id="Td2" runat="server" style=""></td>
            </tr>
            </table>
            </LayoutTemplate>
        <ItemTemplate>
        <tr style="">
        <td>
        <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
        </td>
        <td><asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>' /></td>
        <td><asp:Label ID="lblYear" runat="server" Text='<%# Bind("Year") %>' /></td>
        <td><asp:Label ID="lblGenre" runat="server" Text='<%# Bind("Genre") %>' /></td>
        <td><asp:Label ID="lblCast" runat="server" Text='<%# Bind("Cast") %>' /></td>
        </tr>
        </ItemTemplate>
        <InsertItemTemplate>
        <tr style="">
        <td>
        <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
        </td>       
        <td><asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' /></td>
        <td><asp:TextBox ID="txtYear" runat="server" Text='<%# Bind("Year") %>' /></td>
        <td><asp:TextBox ID="txtGenre" runat="server" Text='<%# Bind("Genre") %>' /></td>
        <td><asp:TextBox ID="txtCast" runat="server" Text='<%# Bind("Cast") %>' /></td>
        <td><asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" /></td>
        </tr>
        </InsertItemTemplate>
        </asp:ListView>   

Code

using System.Data;
using System.Xml;

string xmlfile = @"C:\Documents and Settings\rsaxena.MCNSOLUTIONS\My Documents\Visual Studio 2008\WebSites\ListViewCRUDWithXMl\MovieDB.xml";

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
        }
    }

protected void LoadData()
    {
        DataSet ds = new DataSet();
        DataTable table = new DataTable();
       
        ds.ReadXml(xmlfile);
        MoviesListView.DataSource = ds;
   
        MoviesListView.DataBind();
    }

protected void MoviesListView_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        TextBox txtNameTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtName");
        TextBox txtYearTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtYear");
        TextBox txtGenreTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtGenre");
        TextBox txtCastTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtCast");
       
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlfile);
       XmlElement xelement = xmlDoc.CreateElement("Movie");

         XmlElement xName = xmlDoc.CreateElement("Name");
        xName.InnerText = txtNameTextBox.Text;
        xelement.AppendChild(xName);
       
        XmlElement xYear = xmlDoc.CreateElement("Year");
        xYear.InnerText = txtYearTextBox.Text;
        xelement.AppendChild(xYear);
        XmlElement xGenre = xmlDoc.CreateElement("Genre");
        xGenre.InnerText = txtGenreTextBox.Text;
        xelement.AppendChild(xGenre);
 
        XmlElement xCast = xmlDoc.CreateElement("Cast");
        xCast.InnerText = txtCastTextBox.Text;
        xelement.AppendChild(xCast);
       
        xmlDoc.DocumentElement.AppendChild(xelement);
        xmlDoc.Save(xmlfile);
        LoadData();
    }

static Int16 i = 0;

protected void MoviesListView_ItemEditing(object sender, ListViewEditEventArgs e)
    {
       
        MoviesListView.EditIndex = e.NewEditIndex;
        i = Convert.ToInt16(MoviesListView.EditIndex);
       
        Label lblName = (Label)MoviesListView.EditItem.FindControl("lblName");
        Label lblYear = (Label)MoviesListView.EditItem.FindControl("lblYear");
        Label lblGenre = (Label)MoviesListView.EditItem.FindControl("lblGenre");
        Label lblCast = (Label)MoviesListView.EditItem.FindControl("lblCast");
       
        TextBox txtnameTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtName");
        TextBox txtYearTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtYear");
        TextBox txtGenreTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtGenre");
        TextBox txtCastTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtCast");
        txtnameTextBox.Text = lblName.Text;
        txtYearTextBox.Text = lblYear.Text;
        txtGenreTextBox.Text = lblGenre.Text;
        txtCastTextBox.Text = lblCast.Text;
    }

protected void MoviesListView_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {       
        Label lblName = (MoviesListView.Items[e.ItemIndex].FindControl("lblName")) as Label;
        Label lblYear = (MoviesListView.Items[e.ItemIndex].FindControl("lblYear")) as Label;
        Label lblGenre = (MoviesListView.Items[e.ItemIndex].FindControl("lblGenre")) as Label;
        Label lblCast = (MoviesListView.Items[e.ItemIndex].FindControl("lblCast")) as Label;
       
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlfile);
        XmlNodeList newXMLNodes = xmlDoc.SelectNodes("/Movies/Movie");
       
        string value = lblName.Text;
       
        foreach (XmlNode newXMLNode in newXMLNodes)
        {
            if (newXMLNode.InnerText == value)
            {
                newXMLNode.ParentNode.RemoveChild(newXMLNode);
            }
        }       
        xmlDoc.Save(xmlfile);
        xmlDoc = null;
        LoadData();
    }

protected void MoviesListView_ItemCanceling(object sender, ListViewCancelEventArgs e)
{       
        TextBox txtNameTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtName");
        TextBox txtYearTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtYear");
        TextBox txtGenreTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtGenre");
        TextBox txtCastTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtCast");
        txtNameTextBox.Text = string.Empty;
        txtYearTextBox.Text = string.Empty;
        txtGenreTextBox.Text = string.Empty;
        txtCastTextBox.Text = string.Empty;
}

protected void btnUpdate_Click(object sender, EventArgs e)
    {  
       
        TextBox txtNameTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtName");
        TextBox txtYearTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtYear");
        TextBox txtGenreTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtGenre");
        TextBox txtCastTextBox = (TextBox)MoviesListView.InsertItem.FindControl("txtCast");
       
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(xmlfile);       
       
        XmlNode xmlnode = xmldoc.DocumentElement.ChildNodes.Item(i);
        xmlnode["Name"].InnerText = txtNameTextBox.Text;
        xmlnode["Year"].InnerText = txtYearTextBox.Text;
        xmlnode["Genre"].InnerText = txtGenreTextBox.Text;
        xmlnode["Cast"].InnerText = txtCastTextBox.Text;
       
        xmldoc.Save(xmlfile);
        LoadData();
    }

Now it's time to run the application and see the result.
1.JPG

Image1.

2.JPG


Image2


3.JPG

Image3.

4.JPG

Image4.

COMMENT USING

Trending up