This article will help you to create child nodes with data in XML file automatically on button click from ASPX page in ASP.NET. Besides, you can retrieve data from the XML file node by node into text box, on label control.
XML is really an interesting file that you can use as database in which you can store a set of data. Say for example, in your website project you want to have a page through which a user can enter his name, email ID and his comments. Such data may not be commercial for you to store in your data base but at the same time you want to have it for goodwill purposes. Here is the solution, store it in an XML file and retrieve its node values on a web page, it is as cool as that.
Using the Code
Here is an example to show how it can be done.
Add a page within your solution with three text boxes to enter name (
txtEmail) and comment (
txtComment). Also place a button for submit (
btnSubmit) as shown in the image below. You may add required field validators too.
Add an XML file into your project solution and rename it (StoreUserInfo.xml). Create a root node into it:
On button click, paste the following code:
protected void btnSubmit_Click(object sender, EventArgs e)
XmlDocument oXmlDocument = new XmlDocument();
XmlNode oXmlRootNode = oXmlDocument.SelectSingleNode("records");
XmlNode oXmlRecordNode = oXmlRootNode.AppendChild(
"Name", "")).InnerText = txtName.Text;
"Email", "")).InnerText = txtEmail.Text;
"Comment", "")).InnerText = txtComment.Text;
Note: You have to specify the path of XML file correctly as per your project. In my case, it's D:\Sanju\XMLDb1\StoreUserInfo.xml. It will be different for you. This piece of code will automatically create the child nodes along with its values entered in the text boxes at each click of the submit button.
Once you have successfully submitted the records, you will like to see all the submitted records on different web pages but within the same project solution. For that, you add a new page in your project. Now you can display the data from XML file on text box, label or richtext box wherever you desire. In my example, I will use both text box as well as Label controls. Design of my Display page is the same as the above page with an additional Label control.
Note: On the display page, you will be needing one
HiddenField control to keep count of the node number before each postback occurs so as to track through the child nodes in the XML file.
Copy and paste the following code on the
Click event of the
protected void Page_Load(object sender, EventArgs e)
Int32 j = 0;
Hidint.Value = Convert.ToString(j);
Hidint is the ID of the
HiddenField control on the ASPX page.
private int increment()
string strInput = Hidint.Value;
int incrValue = Convert.ToInt32(strInput);
incrValue += 1;
Hidint.Value = incrValue.ToString();
protected void btnShow4mXML_Click(object sender, EventArgs e)
DataSet ds = new DataSet();
Int32 i = increment();
DataTable dt = ds.Tables;
if (i <= dt.Rows.Count)
dr = dt.Rows[i-1];
txtName.Text = dr["name"].ToString();
txtEmail.Text = dr["email"].ToString();
txtComment.Text = dr["comment"].ToString();
Label1.Text = dr["comment"].ToString();
btnShow4mXML.Enabled = false;
Hope this article has helped you in knowing the XML file better.
- 1st April, 2009: Initial post
The Author holds degrees in Bachelor as well as Master of Computer Application. He firmly believes in the philosophy 'Sound Mind in Sound Body'. He loves Jogging, exersising and you may find him lost in deep thoughts when free.He feels Necessity is the mother of all logics and Programming can't be work but challenge whose output is fun n happiness.Bathroom is the place where he finds heaven.