2013-02-21 70 views
0

我怎麼能返回僅由它的根元素組成的一個非常簡單的XML?如: <?xml version="1.0" encoding="UTF-8"?> <myelement> somevalue </myelement>返回一個純XML(無子元素)與網頁API

問題是沒有子元素,所以我不知道什麼類型的POCO類我應該寫,以獲得該輸出。 我一直想是返回一個字符串,但我最終

<string>somevalue</string> 

我是新來的的WebAPI所以任何建議將不勝感激!

+0

您要自定義從網絡API發送XML? – 2013-02-22 15:13:17

+0

嗨,是的,我正在尋找一種更簡單的方式來返回像第一個片段的自定義XML。 – 2013-02-23 23:09:52

回答

0

我敢打賭,這不是最好的解決方案,但它現在正在爲:

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; 
    } 
}