Convert Database data to XML

The data existing in the relational database can be easily converted into an xml document in the program. The following code illustrates this.

I have used SQLServer database table employee ( with columns empid, first_name,last_name,deptcd,salary and resig_status)

The connection is established using appropriate connection string. The data from the RDBMS table is stored in the dataset dsEmp. The xml class XmlDataDocument is used to convert the data into an xml data file. It takes the dataset as input and converts to xml data.

DataSet dsEmp = new DataSet();
string strCon;
string strSelect;

strCon = "data source=SQLEXPRESS;initial catalog=dbemp;persist security info" +
"=False;user id=sa;password=jadoogar;workstation id=HCL;packet size =4096";
strSelect = "Select empid,first_name,deptcd from employee";

SqlConnection sqlCon = new SqlConnection(strCon);
SqlDataAdapter empAdapter = new SqlDataAdapter(strSelect,sqlCon);
XmlDataDocument empDoc = new XmlDataDocument(dsEmp);


No comments:

Post a Comment