如何从 Web API 方法返回 Xml 数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15366096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return Xml Data from a Web API Method?
提问by The Light
I have a Web Api method which should return an xml data but it returns string:
我有一个 Web Api 方法,它应该返回一个 xml 数据,但它返回字符串:
public class HealthCheckController : ApiController
{
[HttpGet]
public string Index()
{
var healthCheckReport = new HealthCheckReport();
return healthCheckReport.ToXml();
}
}
It returns:
它返回:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<myroot><mynode></mynode></myroot>
</string>
and I have added this mapping:
我添加了这个映射:
config.Routes.MapHttpRoute(
name: "HealthCheck",
routeTemplate: "healthcheck",
defaults: new
{
controller = "HealthCheck",
action = "Index"
});
How to make it return just the xml bits:
如何让它只返回 xml 位:
<myroot><mynode></mynode></myroot>
If I was using just MVC, I could be using the below but Web API doesn't support "Content":
如果我只使用 MVC,我可以使用下面的但 Web API 不支持“内容”:
[HttpGet]
public ActionResult Index()
{
var healthCheckReport = new HealthCheckReport();
return Content(healthCheckReport.ToXml(), "text/xml");
}
I have also added the below codes to the WebApiConfig class:
我还在 WebApiConfig 类中添加了以下代码:
config.Formatters.Remove(config.Formatters.JsonFormatter);
config.Formatters.XmlFormatter.UseXmlSerializer = true;
回答by Darrel Miller
The quickest way is this,
最快的方法是这样
public class HealthCheckController : ApiController
{
[HttpGet]
public HttpResponseMessage Index()
{
var healthCheckReport = new HealthCheckReport();
return new HttpResponseMessage() {Content = new StringContent( healthCheckReport.ToXml(), Encoding.UTF8, "application/xml" )};
}
}
but it is also very easy to build a new XmlContent class that derives from HttpContent to support XmlDocument or XDocument directly. e.g.
但是构建一个从 HttpContent 派生的新 XmlContent 类以直接支持 XmlDocument 或 XDocument 也很容易。例如
public class XmlContent : HttpContent
{
private readonly MemoryStream _Stream = new MemoryStream();
public XmlContent(XmlDocument document) {
document.Save(_Stream);
_Stream.Position = 0;
Headers.ContentType = new MediaTypeHeaderValue("application/xml");
}
protected override Task SerializeToStreamAsync(Stream stream, System.Net.TransportContext context) {
_Stream.CopyTo(stream);
var tcs = new TaskCompletionSource<object>();
tcs.SetResult(null);
return tcs.Task;
}
protected override bool TryComputeLength(out long length) {
length = _Stream.Length;
return true;
}
}
and you can use it just like you would use StreamContent or StringContent, except that it accepts a XmlDocument,
并且您可以像使用 StreamContent 或 StringContent 一样使用它,除了它接受 XmlDocument,
public class HealthCheckController : ApiController
{
[HttpGet]
public HttpResponseMessage Index()
{
var healthCheckReport = new HealthCheckReport();
return new HttpResponseMessage() {
RequestMessage = Request,
Content = new XmlContent(healthCheckReport.ToXmlDocument()) };
}
}

