开发者

XML Response contains HTML encoding on API Response

I created a WCF Service that I'm passing a stream to on a request. The client side code looks like this:

  FileInfo fo = new FileInfo("c:/Downloads/test.xml");
        StreamWriter wo = fo.CreateText();

        开发者_如何学PythonXmlDocument MyXmlDocument = new XmlDocument();
        MyXmlDocument.Load("C:/DataFiles/Integrations/RequestXML.xml");
        byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);

        Uri uri = new Uri("http://localhost:63899/MyRESTServiceImpl.svc/Receive");

        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);

        Request.ContentLength = RequestBytes.Length;

        Request.Method = "POST";

        Request.ContentType = "text/xml";

        Stream RequestStream = Request.GetRequestStream();
        RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
        RequestStream.Close();

        HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string r = reader.ReadToEnd();
        //XmlDocument ReturnXml = new XmlDocument();
        //ReturnXml.LoadXml(reader.ReadToEnd());
        response.Close();

        wo.Write(r);

Right now, all I want to do is process the request and then return the XML right back to the client for testing purposes. Here are my IMyRESTServiceImpl.cs and MyRESTServiceImpl.svc.cs code respectively:

[ServiceContract]
public interface IMyRESTServiceImpl
{
    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
    Stream Receive(Stream text);
}


public class MyRESTServiceImpl : IMyRESTServiceImpl
{

    public Stream Receive(Stream text)  
        {  
            string stringText = new StreamReader(text).ReadToEnd();  

            return text;
        }   

}

Basically what's happening is that the API is returning my XML to me in string tags and using HTML encoding for the < and > signs (& gt; & lt;). I need it to just return the XML to me exactly as it was sent. I've debugged it and the XML remains intact on the server side so this is occurring as it's sent back. Any ideas on how to handle this? Thanks.


The implementation you have doesn't compile - the method is declared to return a Stream, but it's returning a String. If you return as a string, it will encode the XML characters; if you don't want the encoding, return it either as a Stream or as a XmlElement (or XElement).

Update with example

This is an example of a method returning a Stream for an arbitrary XML response:

[WebGet]
public Stream GetXML()
{
    string theXml = @"<products>
  <product name=""bread"" price=""1.33">
    <nutritionalFacts>
      <servings>2</servings>
      <calories>150</calories>
      <totalFat>2</totalFat>
    </nutritionalFacts>
  </product>
  <product name=""milk"" price=""2.99">
    <nutritionalFacts>
      <servings>8</servings>
      <calories>120</calories>
      <totalFat>5</totalFat>
    </nutritionalFacts>
  </product>
</products>";
    WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    MemoryStream result = new MemoryStream(Encoding.UTF8.GetBytes(theXml);
    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜