如何通过基于WCF的Web服务获取XML输出

时间:2013-02-05 16:48:50

标签: c# asp.net xml wcf

我有一个基于WCF的web服务,在方法内部,我正在执行存储过程并填充数据集,然后在最后返回数据集,如同一列和多行。但是我的要求现在不同了,因为我通过其他产品调用这个web服务,这个产品需要基于XML的输出。 (它是一种示例XML格式),但我想以相同的方式。那么我如何生成xml并返回它...如果有人可以在给定代码中进行更正,我将不胜感激,因为我不是XML家伙,这是我第一次处理XML。我想要点什么

<xml>
   <Approvers>
      <Approver>
          <Approvername>John</Approvername>
      </Approver>
   </Approvers>
</xml>

我想要遵循的XML格式示例。

enter image description here

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;
            }
        }

1 个答案:

答案 0 :(得分:0)

正如你在视频中看到的那样,你非常接近于解决这个问题。

确保设置DataTable.TableName,因为如果不这样做,则不会序列化。

剩下的就是setting up the WCF Service并定义WebMethodDataTable返回XML字符串。

设置WebService后,定义WebMethod并返回XML如下所示:

//StringWriter requires System.IO
using System.IO;

//This are hypothetical Class and WebMethod names, of course
public class RequisitionApprovers : System.Web.Services.WebService
{
    [WebMethod]
    public string ReturnRequisitionApproversAsXMLString(DataSet ds)
    {
        StringWriter writer = new StringWriter();
        ds.WriteXml(writer, XmlWriteMode.IgnoreSchema);
        string xmlResult = writer.ToString();

        return xmlResult;
    }
}

现在剩下的就是从需要的xml字符串中删除“\ r \ n”和空格,最后得到一个干净的xml字符串,让WebMethod返回。