将VXML格式用于WCF REST服务响应(XML)格式?

时间:2010-01-26 20:21:25

标签: wcf rest

我使用WCF创建了一个REST Web服务并使用HTTP Post Method。请求和响应对象都是普通的xml。像响应对象一样:

<Response xmlns="http://WebService/WCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Approved>true</Approved>
<ApprovedCode>OK242C0010063439: X:</ApprovedCode>
<ValidationLogID>106</ValidationLogID>
<OrderNumber>D1FB8F95-4B50B216-295-14442</OrderNumber>
<RetString>Approval Code: OK242C0010063439: X:</RetString>
</Response>

现在客户要求我们将格式更改为VoiceXML格式,如下所示:

<?xml version="1.0" ?> 
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
 <form id="Response">
    <var name="Approved" expr="'true'" /> 
    <var name="RetString" expr="'Approval Code: OK242C0010063439: X:'" /> 
    <var name="ApprovedCode" expr="'OK242C0010063439: X:'" /> 
    <var name="ValidationLogID" expr="'106'" /> 
    <var name="OrderNumber" expr="'D1FB8F95-4B50B216-295-14442'" /> 
     <block>
            <return namelist="Approved RetString ApprovedCod ValidationLogID OrderNumber" /> 
    </block>
  </form>
</vxml>

我想知道是否有一种简单的方法可以进行这种转换。目前我的想法是为响应对象构建并返回一个普通字符串而不是XML。

感谢您的帮助! :)

2 个答案:

答案 0 :(得分:2)

更改您的合同以返回Stream并使用XmlWriter写入内存流并返回该内容。

答案 1 :(得分:0)

Darrel,我曾尝试使用Stream,但是当我使用Fiddler 2进行测试时,我只获得了以下内容:

HTTP/1.1 200 OK
Content-Length: 0
Content-Type: application/octet-stream
Server: Microsoft-HTTPAPI/1.0
Date: Wed, 27 Jan 2010 15:37:27 GMT

我使用了MemoryStream,代码如下所示:

    Stream st = new MemoryStream();
    XmlWriterSettings xms = new XmlWriterSettings();
    using (XmlWriter writer = XmlWriter.Create(st, new XmlWriterSettings()))
    {
       //do some writing
       writer.WriteStartElement("vxml", "http://www.w3.org/2001/vxml");
       writer.WriteAttributeString("version", "2.0");
       writer.WriteStartElement("form");
       writer.WriteAttributeString("id", "CompositeType");
       writer.WriteStartElement("var");
       writer.WriteAttributeString("name", "BoolValue");
       writer.WriteAttributeString("expr", composite.BoolValue.ToString());
       writer.WriteEndElement();
    }
    st.Flush(); 
    //this line is necessary, otherwise the returned content is 0
    st.Position = 0; 
    return st;

它没有显示Fiddler中的内容。

编辑:

st.Flush(); 
//this line is necessary, otherwise the returned content is 0
st.Position = 0; 

已添加,响应内容正确显示。