tags:

views:

145

answers:

2

Hi, 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"&gt;
<Approved>true</Approved>
<ApprovedCode>OK242C0010063439: X:</ApprovedCode>
<ValidationLogID>106</ValidationLogID>
<OrderNumber>D1FB8F95-4B50B216-295-14442</OrderNumber>
<RetString>Approval Code: OK242C0010063439: 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"&gt;
 <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! :)

+1  A: 

Change your contract to return a Stream and use an XmlWriter to write to a memory stream and return that.

Darrel Miller
will try that now. Thanks a lot. Will mark as answer if went through :)
Zhao Wang
Hi, Darrel, I have tried to use Stream, but I got no result back except the Response Header show the content-type is application/octet-stream. Any suggestion? Thanks!
Zhao Wang
Hi, Darrel, it works. I just need to set the MemoryStream's position as 0. Thank you very much! Appreciate it.
Zhao Wang
A: 

Hi, 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.

Zhao Wang