For Answers, see/post comments

Convert Database data to XML

The data existing in the relational database can be easily converted into an xml documant 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";

try{
SqlConnection sqlCon = new SqlConnection(strCon);
SqlDataAdapter empAdapter = new SqlDataAdapter(strSelect,sqlCon);
empAdapter.Fill(dsEmp,"employee");
}
catch
{
}

XmlDataDocument empDoc = new XmlDataDocument(dsEmp);
empDoc.Save(MapPath("xmldata/newemp.xml"));
}

No comments: