返回带有Web Api的纯xml(无子元素)

返回带有Web Api的纯xml(无子元素),xml,asp.net-mvc,asp.net-web-api,Xml,Asp.net Mvc,Asp.net Web Api,如何返回仅由根元素组成的非常简单的xml?比如: somevalue 问题是没有子元素,所以我不知道应该编写什么类型的POCO类来获得输出。 我一直在尝试返回一个字符串,但结果是 <string>somevalue</string> somevalue 我是WebApi新手,因此任何建议都将非常感谢 我打赌这不是最好的解决方案,但它目前正在发挥作用: public class MyCustomFormatter : ActionFilterAttribute {

如何返回仅由根元素组成的非常简单的xml?比如:
somevalue

问题是没有子元素,所以我不知道应该编写什么类型的POCO类来获得输出。 我一直在尝试返回一个字符串,但结果是

<string>somevalue</string>
somevalue

我是WebApi新手,因此任何建议都将非常感谢

我打赌这不是最好的解决方案,但它目前正在发挥作用:

public class MyCustomFormatter : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        HttpResponseMessage response = actionExecutedContext.Response;
        var contentType = response.Content.Headers.ContentType;
        var oldContents = response.Content.ReadAsStringAsync().Result;
        oldContents = oldContents.Replace("string", "myelement");
        response.Content = new StringContent("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + oldContents);
        response.Content.Headers.ContentType = contentType;
        actionExecutedContext.Response.Content = response.Content;
    }
}
公共类MyCustomFormatter:ActionFilterAttribute
{
公共覆盖无效OnActionExecuted(HttpActionExecuteContext ActionExecuteContext)
{
HttpResponseMessage response=actionExecutedContext.response;
var contentType=response.Content.Headers.contentType;
var oldContents=response.Content.ReadAsStringAsync().Result;
oldContents=oldContents.Replace(“字符串”、“myelement”);
response.Content=newstringcontent(“+oldContents”);
response.Content.Headers.ContentType=ContentType;
actionExecutedContext.Response.Content=Response.Content;
}
}

要自定义从Web Api发送的Xml吗?您好,是的,我正在寻找一种更简单的方法来返回自定义Xml,就像第一个代码段中的方法一样。