到的WebAPI返回XML [英] WebAPI to Return XML

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

问题描述

我想我的Web API方法返回XML对象返回给调用应用程序。目前,它只是返回XML作为一个字符串对象。这是一种没有没有?如果是的话你怎么告诉的WebAPI get方法,它的返回类型的XML对象?

I'm wanting my WEB API method to return an XML object back to the calling application. Currently it's just returning the XML as a string object. Is this a no no? If so how do you tell the webapi get method that it's returning an object of type XML?

感谢

编辑:Get方法的一个例子:

An example of the Get method:

[AcceptVerbs("GET")]
public HttpResponseMessage Get(int tenantID, string dataType, string ActionName)
{
   List<string> SQLResult = MyWebSite_DataProvidor.DB.spReturnXMLData
            ("SELECT * FROM vwContactListing FOR XML AUTO, ELEMENTS").ToList();
   string AllResults = "";
   for (int i = 0; i < SQLResult.Count - 1; i++)
   {
       AllResults += SQLResult[i];
   }
    string sSyncData = "<?xml version=\"1.0\"?> " + AllResults;
    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StringContent(sSyncData);
    return response;          
}

它有点哈克由于IM仍然处于原型阶段。将重构时,我可以证明它是可行的。

Its a bit hacky because im still at the prototyping stage. Will refactor when i can prove its doable.

推荐答案

如果您返回一个序列化对象,的WebAPI会自动根据您的客户端发送Accept头发送JSON或XML。

If you return a serializable object, WebAPI will automatically send JSON or XML based on the Accept header that your client sends.

如果您返回一个字符串,你会得到一个字符串。

If you return a string, you'll get a string.

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

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