How to: Generate XML file from database table
2011-08-25 10:45
459 查看
In this article, I am going to show how we can generate a XML file from our Sql Server database. I am going to explain this task with a simple example.
The default.aspx.cs code is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
string XMLFileName = "D:/XmlFileFromDataBaseData";
string connString = "Data Source=Local; Initial Catalog=northwind; Uid=sa; pwd=";
string Query = "select * from Employees";
XMLGenarate(connString, Query, XMLFileName);
Response.Write(XMLFileName);
}
public void XMLGenarate(string connstring, string Query, string FileName)
{
string XMLFileName = FileName;
con = new SqlConnection(connstring);
da.SelectCommand = new SqlCommand(Query, con);
da.Fill(ds);
FileStream fs = null;
fs = new FileStream(XMLFileName + ".xml", FileMode.OpenOrCreate, FileAccess.Write);
ds.WriteXml(fs);
fs.Close();
}
}
Here in this application, I am using Employees table of northwind DataBase. The location for generated XML file is "D:/". When user run the application then we can see the XML file has been generated. The output of the XML file will look something like this:
The default.aspx.cs code is:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
string XMLFileName = "D:/XmlFileFromDataBaseData";
string connString = "Data Source=Local; Initial Catalog=northwind; Uid=sa; pwd=";
string Query = "select * from Employees";
XMLGenarate(connString, Query, XMLFileName);
Response.Write(XMLFileName);
}
public void XMLGenarate(string connstring, string Query, string FileName)
{
string XMLFileName = FileName;
con = new SqlConnection(connstring);
da.SelectCommand = new SqlCommand(Query, con);
da.Fill(ds);
FileStream fs = null;
fs = new FileStream(XMLFileName + ".xml", FileMode.OpenOrCreate, FileAccess.Write);
ds.WriteXml(fs);
fs.Close();
}
}
Here in this application, I am using Employees table of northwind DataBase. The location for generated XML file is "D:/". When user run the application then we can see the XML file has been generated. The output of the XML file will look something like this:
相关文章推荐
- Generate the java class from XML Schema file using JAXB
- 94.You plan to move data from a flat file to a table in your database. You decide to use SQL*Loader
- Generate the java class from XML Schema file using JAXB
- Generate java file from XSD, missing @XMLRootElement annotation.
- Using Databases with Python -Musical Track Database (produce database from XML file)
- java genrate xml file use data from database
- Generate class from database table How can i generate a class from a table at a SQL Server?
- Generate the java class from XML Schema file using JAXB
- How to: Read Object Data from an XML File
- Retore database from a back up file.
- KettleXMLException: 错误从XML文件读取转换 (error during reading transform from the xml file )
- Use jQuery To Retrieve Data From An XML File
- .Net: handle exceptions while loading dataset from xml file
- A real and useful way to get/prase XML file from a site
- 139 Which three statements must be true before transporting a tablespace from a database on one plat
- Spring Batch Example – XML File To MongoDB Database(五)
- [ERROR]-Error: failure: repodata/filelists.xml.gz from addons: [Errno 256] No more mirrors to try.
- Copy a table from one database to another in SQL Server 2005
- trailing nulcols:difference between external table and sqlldr:create external table from controlfile
- File res/drawable/abc_vector_test.xml from drawable resource ID