您正在使用不是您想要的XMLSerializer序列化String對象。
你可以做到這一點,
public IHttpActionResult Get(int id)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("test.xml");
return new ResponseMessageResult(new HttpResponseMessage() {Content = new StringContent(xmlDoc.InnerXml, Encoding.UTF8,"application/xml")});
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
,或者你可以創建自己的IHttpActionResult助手類,這樣,
public class XmlResult : IHttpActionResult
{
private readonly XmlDocument _doc;
public XmlResult(XmlDocument doc)
{
_doc = doc;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return
Task.FromResult(new HttpResponseMessage()
{
Content = new StringContent(_doc.InnerXml, Encoding.UTF8, "application/xml")
});
}
}
那麼這將允許你這樣做,
public IHttpActionResult Get(int id)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("test.xml");
return new XmlResult(xmlDoc);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
或者你可以切換到XElement,它會神奇地做正確的事情,
public IHttpActionResult Get(int id)
{
try
{
XElement xElement = XElement.Load(..);
return OK(xElement);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}
你可以找到爲什麼行爲的方式,ISIN這篇博客文章的詳細信息http://www.bizcoder.com/posting-raw-json-to-web-api
你在哪裏看到的身體嗎?在調試器中?在實體中不可能包含'\「',請嘗試使用」查看源代碼「來確定它的外觀 – 2014-10-27 17:35:41
我正在使用Fiddler進行測試,並且可以看到原始消息 – user3616544 2014-10-27 17:37:13
我建議您查看源代碼返回XML後的頁面'''',並且引用它是創建XML的奇怪方式。 – 2014-10-27 17:39:22