How to store data from text box into xml file.
XML can be used as database to store information.This resource explains about the application which stores the data from textbox.
We can create the application form with different textbox and can store the data in XML file instead of any database like access or sql. Learn how to store data from text box into xml file.
Find how to store data from text box into xml file?
In this application there are two textbox for Name and Age which takes input from user. There are two buttons one to create XML and other to save the textbox data into xml.
Below is the code for application:
1) Creating XML :
private void create_xml_Click(object sender, EventArgs e)
{
xmltxt = new XmlTextWriter("C:\\"+ "Sample.xml", null);
xmltxt.WriteStartDocument();
xmltxt.WriteStartElement("Database");
xmltxt.WriteStartElement("Data");
xmltxt.WriteStartElement("Name");
xmltxt.WriteString("");
xmltxt.WriteFullEndElement();
xmltxt.WriteStartElement("ID");
xmltxt.WriteString("");
xmltxt.WriteFullEndElement();
xmltxt.WriteFullEndElement();
xmltxt.WriteFullEndElement();
xmltxt.WriteEndDocument();
xmltxt.Close();
}
2) Save Data :
This code will save the textbox data into xml:
private void save_xml_Click(object sender, EventArgs e)
{
string XMLFilePath = "C:\\Sample.xml";
DataSet ds = new DataSet();
ds.ReadXml(XMLFilePath);
DataRow NewRow = ds.Tables[0].NewRow();
NewRow["Name"] = textBox1.Text;
NewRow["Id"] = textBox2.Text;
ds.Tables[0].Rows.Add(NewRow);
if (ds.Tables[0].Rows[0].IsNull(0) && ds.Tables[0].Rows[0].IsNull(1))
{
ds.Tables[0].Rows[0].Delete();
}
ds.WriteXml(XMLFilePath);
}
If you want to check the XML data , then we can assign that XML data to the datagridview using below code:
private void load_xml_Click(object sender, EventArgs e)
{
try
{
DataSet ds1 = new DataSet();
ds1.ReadXml("D:\\Response.xml");
ds1.Tables[0].Rows.RemoveAt(0);
dataGridView1.DataSource = ds1.Tables[0];
dataGridView1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex.Message);
}
}
Hope this application we help you .
If you require any change in this you may ask for that.