Reader Level:
ARTICLE

Read and Write XML to Grid View

Posted by Vijay Prativadi Articles | C# Language December 17, 2011
Today, in this article let see how to create the XML file accepting the values from user using textboxes.
  • 1
  • 0
  • 8871

Today, in this article let see how to create the XML file accepting the values from user using textboxes. The values passed from textboxes are converted into xml format and stored in xml file. So here we are writing into xml file and later in next session based on the created date in xml file we need to show the data to a grid such that it data which is in grid can be read through a UI grid.

Code Toolbox Requirements:

  • 5 Labels, 1 Gridview, 1 Button and 4 Textboxes.

Here I have taken 2 methods bind and data, where the bind is used to write to xml document and data to retrieve the values from xml file.

The Complete Code of Default.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Xml_Grid_Write_and_Read.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 id="Head1" runat="server">
    <title></title
>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <center>
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Please Enter Id" Font-Bold="true" Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Please Enter FirstName" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="Please Enter LastName" Font-Bold="true"
                            Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label4" runat="server" Text="Please Enter Age" Font-Bold="true" Font-Italic="true"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Click Here " OnClick="Button1Click" />
                    </td>
                </tr>
            </table>
        </center>
        <center>
            <table>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC"
                            BorderStyle="None" BorderWidth="1px" CellPadding="3" EnableModelValidation="True"
                            AutoGenerateColumns="False" OnLoad="PageLoad">
                            <FooterStyle BackColor="White" ForeColor="#000066" />
                            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                            <RowStyle ForeColor="#000066" />
                            <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                            <Columns>
                                <asp:BoundField HeaderText="Person Id" DataField="PersonId" />
                                <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                                <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                                <asp:BoundField HeaderText="Age" DataField="Age" />
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </center>
        <center>
            <asp:Label ID="Label5" runat="server" Font-Size="Larger"></asp:Label>
        </center>
    </div>
    </form
>
</body>
</
html>

The Complete Code of Default.aspx.cs looks like this:

using System;
using System.Xml;
using System.Data;

namespace Xml_Grid_Write_and_Read
{
    public partial class Default : System.Web.UI.Page
    {
        protected void PageLoad(object sender, EventArgs e)
        {
            Data();
            TextBox1.Focus();

        }
        protected void Bind()
        {

            var doc = new XmlDocument();

            var declaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(declaration);

            var rootelement = doc.CreateElement("Student");
            doc.AppendChild(rootelement);

            var personelement = doc.CreateElement("Person");
            rootelement.AppendChild(personelement);

            var personid = doc.CreateElement("PersonId");
            personid.InnerText = TextBox1.Text;
            personelement.AppendChild(personid);

            var firstName = doc.CreateElement("FirstName");
            firstName.InnerText = TextBox2.Text;
            personelement.AppendChild(firstName);

            var lastName = doc.CreateElement("LastName");
            lastName.InnerText = TextBox3.Text;
            personelement.AppendChild(lastName);

            var age = doc.CreateElement("Age");
            age.InnerText = TextBox4.Text;
            personelement.AppendChild(age);

            doc.Save(Server.MapPath("~/XMLFile1.xml"));

            if (true)
            {
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";

                Label5.ForeColor = System.Drawing.Color.Brown;
                Label5.Text = "<center><b><i></br> The Records are Inserted </br></i></b></center>";
            }
        }

        protected void Data()
        {
            var ds = new DataSet();
            var path = Server.MapPath("~/XMLFile1.xml");
            ds.ReadXml(path);
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();

        }
        protected void Button1Click(object sender, EventArgs e)
        {
            Bind();
        }

    }
}



The Complete Description of XMLFile1.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Student>
  <
Person>
    <
PersonId>1</PersonId>
    <FirstName>Vijay</FirstName>
    <LastName>Prativadi</LastName>
    <Age>25</Age>
  </Person>
  <
Person>
    <
PersonId>2</PersonId>
    <FirstName>Ram</FirstName>
    <LastName>Reddy</LastName>
    <Age>26</Age>
  </Person>
  <
Person>
    <
PersonId>3</PersonId>
    <FirstName>Swetha</FirstName>
    <LastName>Prativadi</LastName>
    <Age>27</Age>
  </Person>
  <
Person>
    <
PersonId>4</PersonId>
    <FirstName>Rahul</FirstName>
    <LastName>Satelli</LastName>
    <Age>25</Age>
  </Person>

</Student>


The Complete Output of the Application looks like this:

After Data is inserted:


Data Records Inserted.png

After Data is retrieved:


Data Records Grid.png


I hope this article is useful for you.

COMMENT USING