网页API返回的XML [英] Web API return XML

查看:253
本文介绍了网页API返回的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示返回的数据作为XML,但它以纯文本格式返回。我有这个code:

I'm trying to display the returned data as xml, but it returns in plain text. I've this code:

context.Response.AddHeader("Content-Type", "text/xml");
context.Response.Write("<pre>" + HttpUtility.HtmlEncode(writer) + "</pre>");

使用这个我':

using (XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8))
                            {

... write the xml

}

...并创建XML。

... and create the XML.

这是我怎么送: context.Response.Write(&LT; pre&gt;中+ HttpUtility.HtmlEn code(作家)+&LT; / pre&GT ;);

如何在RAW返回XML与代码和所有?

How can i return XML in RAW with tags and all?

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.httpconfiguration.formatters%28v=vs.118%29.aspx\"相对=nofollow> HttpConfiguration.Formatters 包含各种格式化序列化模型。而是直接写入响应,考虑你的行动来回报模型或的XDocument ,并确保您有 XmlFormatter HttpConfiguration.Formatters 设置为实际序列为XML。

HttpConfiguration.Formatters contains various formatters to serialize your model. Instead of writing directly to Response, consider your action to return a model or XDocument, and then make sure that you have XmlFormatter set in HttpConfiguration.Formatters to actually serialize it as XML.

这篇关于网页API返回的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆