我有一个基于 WCF 的 Web 服务,在该方法中我正在执行一个存储过程并填充数据集,然后最后返回数据集......就像一列和多行一样。但是我的要求现在不同了,因为我通过其他一些产品调用这个 web 服务,它期望像这样基于 XML 的输出。(它是一个示例 XML 格式)但我想要以相同的方式。那么我如何生成 xml 并将其返回..如果有人可以在给定的代码中进行更正,我将不胜感激,因为我不是 XML 人,这是我第一次处理 XML。我想要一些东西
<xml>
<Approvers>
<Approver>
<Approvername>John</Approvername>
</Approver>
</Approvers>
</xml>
我想遵循的示例 XML 格式。
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;
}
}