C# 转义从我的API方法传入的转义字符

C# 转义从我的API方法传入的转义字符,c#,xml,api,asp.net-web-api,C#,Xml,Api,Asp.net Web Api,生成xml的API方法如下所示(很好) 我从哪里得到了回应 string api_response = client.UploadString(myurl, myRequest) 上面我的api_响应字符串如下所示 "\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?> 读取返回格式正确的xml: 只有在我创建了Response之后,额外的反斜杠才会出现(通过单击放大镜选项查看字符串) 当我从api返回响应时(作为interne

生成xml的API方法如下所示(很好)

我从哪里得到了回应

string api_response = client.UploadString(myurl, myRequest)
上面我的api_响应字符串如下所示

"\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>
读取返回格式正确的xml: 只有在我创建了Response之后,额外的反斜杠才会出现(通过单击放大镜选项查看字符串)

当我从api返回响应时(作为internet explorer上的文本),我的实际文本如下“ 找到了答案 用ResponseMessage替换我的

var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Accepted) {
      RequestMessage = Request,
      Content = new StringContent(content)
    };
    return ResponseMessage(httpResponseMessage);
好的,换了

 return Request.CreateResponse(HttpStatusCode.OK, res);


不清楚您在哪里查看字符串,或者您在使用它做什么。您的原始XML是否真的包含反斜杠?它不应该包含反斜杠。您真的在调试器中查看字符串吗?基本上我们需要更多信息。@JonSkeet是的,我在调试器中查看字符串。在底部添加了更多详细信息,所以查看调试器中的字符串已经添加了一层转义。我建议您将文本转储到文件或控制台。如果
Request.CreateResponse
添加了任何内容,我会非常惊讶…@JonSkeet我理解它在调试器中添加了一层转义,但是添加3个反斜杠太多了。这就像是在转义escape key您确实需要找出响应中的内容。您是否能够使用Wireshark或Fiddler2查看原始HTTP?然后您可以确定这是客户端问题还是服务器端问题。
  HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "text/xml; charset=utf-8";
            httpRequest.Accept = "text/xml";
            httpRequest.Credentials = new NetworkCredential(userName, password);
            byte[] bytesToWrite = Encoding.UTF8.GetBytes(root.ToString());

            using (Stream st = httpRequest.GetRequestStream())
            {
                st.Write(bytesToWrite, 0, bytesToWrite.Length);
            }

            HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
            string read = reader.ReadToEnd();
            return read;
var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Accepted) {
      RequestMessage = Request,
      Content = new StringContent(content)
    };
    return ResponseMessage(httpResponseMessage);
 return Request.CreateResponse(HttpStatusCode.OK, res);
var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Accepted) {
      RequestMessage = Request,
      Content = new StringContent(content)
    };
    return ResponseMessage(httpResponseMessage);