我使用自定义 ActionResult 从控制器 Action 返回 XML:
public class XmlActionResult : ActionResult
{
/// <summary>
/// This class is a custom ActionResult that outputs the content of an XML document to the response stream
/// </summary>
private readonly XDocument _document;
public Formatting Formatting { get; set; }
public string MimeType { get; set; }
public XmlActionResult(XDocument document)
{
_document = document;
MimeType = "text/xml";
Formatting = Formatting.None;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = MimeType;
using(var writer = new XmlTextWriter(context.HttpContext.Response.OutputStream, null)
{
Formatting = Formatting
})
_document.WriteTo(writer);
}
}
这会将 XML 树输出到浏览器。我有一个转换 XML 的 XSL 文件,如何将样式表应用于 XML 输出?