I have a WCF based webservice and inside the method I am executing a stored procedure and filling dataset then at the end returning the dataset back..like one column and multiple rows. But my requirement is different now as I am calling this webservice through some other product which expects the output based on XML like this. (its a sample XML format) but i want something in the same manner. so how do i generate the xml and return it..I would appreciate if someone can make corrections in the given code as I am not XML guy this is the first time I am going to deal with XML. I would like something
<xml>
<Approvers>
<Approver>
<Approvername>John</Approvername>
</Approver>
</Approvers>
</xml>
Sample XML format which i want to follow.

public DataSet getRequisitionApprovers(string pEmail, string pLocationType)
{
//Read Datasource properties from Web.config file to access Oracle EBS Database
string SDataSource = System.Configuration.ConfigurationManager.AppSettings["SQLDataSource"].ToString();
string SUserID = System.Configuration.ConfigurationManager.AppSettings["SQLUserID"].ToString();
string SPassword = System.Configuration.ConfigurationManager.AppSettings["SQLPassword"].ToString();
//Build connection string based on retrieved parameters from web.config file
string connectionString = "Data Source=" + SDataSource + ";Persist Security Info=True;" + "User ID=" + SUserID + ";Password=" + SPassword;
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
// Name of procedure or function to be execu
command.CommandText = "Get_RequisitionApprovers";
command.Parameters.Add(new SqlParameter("theEmail", SqlDbType.VarChar)).Value = pEmail;
command.Parameters.Add(new SqlParameter("LocationType", SqlDbType.VarChar)).Value = pLocationType;
//Create New DataSET & DataTable
DataSet dsApprovers = new DataSet();
DataTable dtApprovers = new DataTable();
//Create DataTable Columns and define the data type
dtApprovers.Columns.Add("Approver1", typeof(string));
//Add DataTable to DataSource
dsApprovers.Tables.Add(dtApprovers);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//Create new Data Row
DataRow theRow = dsApprovers.Tables[0].NewRow();
theRow[0] = reader[0].ToString() ; //Add -> Approver
dsApprovers.Tables[0].Rows.Add(theRow);
}
return dsApprovers;
}
}
As you can see in the video, you’re pretty close to figuring this out.
Make sure you set the
DataTable.TableNamebecause if you don’t, it won’t serialize.All that remains is setting up the WCF Service and defining a
WebMethodreturning an XML string from yourDataTable.After setting up your WebService, defining a WebMethod and returning the XML goes like this:
All that’s left now is to remove the “\r\n” and the whitespaces from the xml string where needed and you end up with a clean xml string to have your
WebMethodreturn.