WebAPI 返回 XML [英] WebAPI to Return 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 方法的示例:
[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;
}
它有点老套,因为我还处于原型设计阶段.当我能证明它可行时将重构.
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屋!