Monday 17 February 2014

XML example in asp.net


XML example in asp.net  OR  XML CRUD operation in asp.net 


Introduction:

Here I will explain how to use XML in asp.net using c# or how to work with XML using asp.net with C#. If you want to create simple XML file with data in asp.net using c# then do following step.

we can perform CRUD operation on XML file.

So, Now I explaining CRUD operation on XML file in asp.net using c#.

Description: 

In previously post I explained to  
Three tier architecture in asp.net
Remove .aspx from url in asp.net
Temp table in sql
Change the column name or datatype or size in sql server
Stored procedure with paging sql server
Calling web service without adding web reference

-- > If you want to
1) insert data in XML OR
2) Update data in XML OR
3) Delete data in XML OR
4) Display data from XML file

then Do following

--> following code  paste in your .aspx file
------------------------------------------------------------------------------------------------

  

<div>
<table border="1">
<tr>
    <th>ID : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox1" />
    </td>
</tr>
<tr>
    <th>FirstName : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox2" />
    </td>
</tr>
<tr>
    <th>Last Name : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox3" />
    </td>
</tr>
<tr>
    <th>City Name : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox4" />
    </td>
</tr>
<tr>
    <th>Pincode : </th>
    <td>
        <asp:TextBox runat="server" ID="TextBox5" />
    </td>
</tr>
<tr>
<th colspan="2">
<asp:Button Text="Insert" ID="Button1" runat="server" onclick="Button1_Click" />
&nbsp;<asp:Button Text="Update" ID="Button2" runat="server"
    onclick="Button2_Click" />
&nbsp;<asp:Button Text="Delete" ID="Button3" runat="server"
    onclick="Button3_Click" />
</th>               
</tr>
<tr>
    <th colspan="2">
        <asp:GridView runat="server"  ID="gv" />
            </asp:GridView>
    </th>
</tr>
</table>
</div>



--> paste following namespace in your code behind
------------------------------------------------------------------------------------------------


using System.Xml;  //required namespace
using System.IO;    //required namespace

--> paste following code  in your page load
------------------------------------------------------------------------------------------------

    

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


--> And also copy all following event in your code behind file
------------------------------------------------------------------------------------------------

   

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (File.Exists(Server.MapPath("StudentDetail.xml")) == false)
        {
            CreateNewFileStruct();
            //if file not exist then create file
        }
        else
        {
            InsertNewRecord();
            // if file exist then insert record in xml file
        }
    }

    private void InsertNewRecord()
    {
    //this is for insert new record
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("StudentDetail.xml"));

        XmlElement Student = xdoc.CreateElement("Student");

        XmlElement ID = xdoc.CreateElement("ID");
        XmlText xmlID = xdoc.CreateTextNode(TextBox1.Text);

        XmlElement FirstName = xdoc.CreateElement("FirstName");
        XmlText xmlFirstName = xdoc.CreateTextNode(TextBox2.Text);

        XmlElement LastName = xdoc.CreateElement("LastName");
        XmlText xmlLastName = xdoc.CreateTextNode(TextBox3.Text);

        XmlElement City = xdoc.CreateElement("City");
        XmlText xmlCity = xdoc.CreateTextNode(TextBox4.Text);

        XmlElement Pincode = xdoc.CreateElement("Pincode");
        XmlText xmlPincode = xdoc.CreateTextNode(TextBox5.Text);

        ID.AppendChild(xmlID);
        FirstName.AppendChild(xmlFirstName);
        LastName.AppendChild(xmlLastName);
        City.AppendChild(xmlCity);
        Pincode.AppendChild(xmlPincode);

        Student.AppendChild(ID);
        Student.AppendChild(FirstName);
        Student.AppendChild(LastName);
        Student.AppendChild(City);
        Student.AppendChild(Pincode);

        xdoc.DocumentElement.AppendChild(Student);

        xdoc.Save(Server.MapPath("StudentDetail.xml"));
        FillGridView();
    }
    private void FillGridView()
    {
    //this is for fill gridview
        System.Data.DataSet ds = new System.Data.DataSet();
        ds.ReadXml(Server.MapPath("StudentDetail.xml"));
        gv.DataSource = ds;
        gv.DataBind();
    }

    private void CreateNewFileStruct()
    {
    //this is create a new file
        string path = Server.MapPath("StudentDetail.xml");
        XmlTextWriter xtw = new XmlTextWriter(path, null);
        xtw.WriteStartDocument();
       
        xtw.WriteStartElement("Dataset");
        xtw.WriteStartElement("Student");

        xtw.WriteElementString("ID", TextBox1.Text);
        xtw.WriteElementString("FirstName", TextBox2.Text);
        xtw.WriteElementString("LastName", TextBox3.Text);
        xtw.WriteElementString("City", TextBox4.Text);
        xtw.WriteElementString("Pincode", TextBox5.Text);

        xtw.WriteEndElement();
        xtw.WriteEndElement();

        xtw.WriteEndDocument();
        xtw.Close();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
    //this is for one Delete record from XML file
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("StudentDetail.xml"));

        XmlNodeList list =  xdoc.SelectNodes("/Dataset/Student");

        foreach (XmlNode item in list)
        {
            if (item.ChildNodes[0].InnerText == TextBox1.Text)
            {
                item.ParentNode.RemoveChild(item);
            }
        }

        xdoc.Save(Server.MapPath("StudentDetail.xml"));
        FillGridView();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    //this is for update record in XML file
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(Server.MapPath("StudentDetail.xml"));

        XmlNodeList list = xdoc.SelectNodes("/Dataset/Student");

        foreach (XmlNode item in list)
        {
            if (item.ChildNodes[0].InnerText == TextBox1.Text)
            {
                item.ChildNodes[1].InnerText = TextBox2.Text;
                item.ChildNodes[2].InnerText = TextBox3.Text;
                item.ChildNodes[3].InnerText = TextBox4.Text;
                item.ChildNodes[4].InnerText = TextBox5.Text;
            }
        }
        xdoc.Save(Server.MapPath("StudentDetail.xml"));
        FillGridView();
    }

No comments:

Post a Comment



Asp.net tutorials