Hm..after I think for a while, i decide to post this simple program..
But I just want to share my knowledge. Actually I got this from internet too..but I just hope, just hope that I can help someone who really want to make xml from database.
Okey, If I want to make xml from database and show it in GridView (in case for web application /asp.net), these are the steps..
- Create your new Web site application and then open Default.aspx.cs or double click on Default.aspx. On formLoad type this code (better you type so you can more understand it):
Include these library:using System.Data.SqlClient;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;protected void Page_Load(object sender, EventArgs e) {
DataSet ds = new DataSet();
String constr = “Data Source = (local); Initial Catalog=[Choose ur db name] User ID = sa;
Password = [ur db passwd]“;
SqlConnection newConn = new SqlConnection(constr);
String strcmd = “select * from [ur table]“;
SqlDataAdapter da = new SqlDataAdapter(strcmd, newConn);
da.Fill(ds, “[ur table]“);
ds.WriteXml(MapPath(“FileName.xml”));
} - Then create the interface by dragging GridView and a Button from toolbox to Default.aspx.
- Double click on your button (it will show file Default.aspx.cs) and type this code
protected
void Button1_Click(object sender, EventArgs e) {
XmlDataDocument doc = null;
DataSet ds = new DataSet();
string path = Path.Combine(Request.PhysicalApplicationPath, “FileName.xml”);
ds.ReadXml(path);
doc = new XmlDataDocument(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
} - Build your web site and then press the button, the data will show in the GridView. Good Luck!
If you want to use Windows Application, it’s very simple. You just need to change the code inside the Button event. Look at this code
- Type this code inside button1 event
private
void button1_Click(object sender, EventArgs e) {
XmlDataDocument doc = null;
DataSet ds = new DataSet();
ds.ReadXml(Application.StartupPath + @”\FileName.xml”);
doc = new XmlDataDocument(ds);
dataGridView1.DataSource = ds.Table[0];
} - That’s all, but don’t forget to move your xml file to bin\Debug\FileName.xml. Good Luck for you guys.
Remember this is just one of the way to create xml from database and show it in GridView, there are so many ways. I hope these simple code can help you.
