"how to use XML in asp.net"
In this we will see how we can read data from databasae using XmlDataReader and read and write XML files. We will also see how to xcreate the xml schema and vie wit.
Code on Form 1 page:
In this form theri are 3 buttons one will read data from database table using XmlDataReader, one will wrote these file as XML file and then we will read that XML file.
/* This code is written on show data button it will read data from database using XmlDataReader class. */
private void butShowData_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=sugandha;initial catalog=dotnet;uid=sa;pwd=sugandha");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Employee for xml auto",con);
XmlReader xdr = cmd.ExecuteXmlReader();
while (xdr.Read())
{
listBoxShowData.Items.Add(xdr["Emp_id"] + " " + xdr["Emp_name"] + " " + xdr["Emp_dept"] + " " + xdr["Emp_salary"]);
}
xdr.Close();
con.Close();
}
/* this code is writeen on write XMl button which will create a xml file and write the data from listbox to this xml document*/
private void butWriteXML_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=sugandha;initial catalog=dotnet;uid=sa;pwd=sugandha");
SqlDataAdapter adpt = new SqlDataAdapter("select * from Employee", con);
DataSet ds = new DataSet();
adpt.Fill(ds, "Employee");
ds.WriteXml("z:\\Employee_onlyxml.xml", XmlWriteMode.IgnoreSchema);
MessageBox.Show("XML file is generated");
}
/* this will read data from XML file that we have created previously.*/
private void butReadXml_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml("z:\\Employee_onlyxml.xml");
dataGridViewShowXml.DataSource = ds;
dataGridViewShowXml.DataMember = "Employee";
}
Now this is the code for form2:
In this we will first show the data of table in the datagridview and then perform differen operation on it. I.e. we will create an xml file and xml schema and also see this file and schema in the richtextbox.
/*This is the code of saving the data from in the datagridview int he XML file.*/
private void butSaveAsXml_Click(object sender, EventArgs e)
{
DataSet ds=CreateDataSet();
ds.WriteXml(@"z:\asp.net\XML examples\demo_XML_form\Product.xml", XmlWriteMode.IgnoreSchema);
butViewXml.Visible = true;
}
private DataSet CreateDataSet()
{
SqlConnection con = new SqlConnection("server=sugandha;initial catalog=dotnet;uid=sa;pwd=sugandha");
SqlDataAdapter adpt = new SqlDataAdapter("select pid,pname,price from copyofproduct", con);
DataSet ds = new DataSet();
adpt.Fill(ds, "copyofproduct");
return ds;
}
/*in this we will see the data from xml file in the richtextbox.We have used XmlReader to read the dat from file.*/
private void butViewXml_Click(object sender, EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXml(@"z:\asp.net\XML examples\demo_XML_form\Product.xml");
FileStream fs = new FileStream(@"z:\asp.net\XML examples\demo_XML_form\Product.xml", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
richTextBoxReadXml.Text = sr.ReadToEnd();
richTextBoxReadXml.Visible = true;
sr.Close();
fs.Close();
}
private void Form2_Load(object sender, EventArgs e)
{
//richTextBoxReadXml.Visible = false;
}
/* this will save the Xml schema*/
private void butSaveSchema_Click(object sender, EventArgs e)
{
DataSet ds = CreateDataSet();
ds.WriteXml(@"z:\asp.net\XML examples\demo_XML_form\Product.xsd", XmlWriteMode.WriteSchema);
butViewSchema.Visible = true;
}
/*this will show the xml schema*/
private void butViewSchema_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(@"z:\asp.net\XML examples\Product.xsd");
FileStream fs = new FileStream(@"z:\asp.net\XML examples\demo_XML_form\Product.xsd", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
richTextBoxReadXml.Text = sr.ReadToEnd();
richTextBoxReadXml.Visible = true;
sr.Close();
fs.Close();
}
private void richTextBoxReadXml_TextChanged(object sender, EventArgs e)
{
}
private void button_showdata_Click(object sender, EventArgs e)
{
DataSet ds = CreateDataSet();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "copyofproduct";
butSaveAsXml.Visible = true;
butSaveSchema.Visible = true;
}
Hi sugandha,
Thanks, Good Article.
Thanks,
Ram