Use VXML format for WCF REST Service Response (XML) Format?
I have created a REST web service using WCF and use HTTP Post Method. The request and response objects are all plain xml. Like the response object is:
<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: OK242C001006开发者_开发知识库3439: X:</RetString>
</Response>
Now the Client ask us to change the format to VoiceXML format like below:
<?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>
I am wondering if there is a simple way to do this transformation. Currently what I am thinking is to build and return an plain string instead of XML for the response object.
Thank you for your help! :)
Change your contract to return a Stream and use an XmlWriter to write to a memory stream and return that.
Darrel, I have tried to use Stream, but when I test with Fiddler 2, I only got the following contents:
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
I used MemoryStream, code is shown below:
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;
It does not show the contents in Fiddler.
Edit:
st.Flush();
//this line is necessary, otherwise the returned content is 0
st.Position = 0;
is Added and the response content show correctly.
精彩评论