2013-04-21 40 views
0

看XML數據我當我使用asp.net網站的API和eventhough我個人喜歡用HttpResponseMessageRequest.CreateResponse我可以讓客戶來決定他們寧可在上班,以處理我的JSON數據。如何使用Request.CreateResponse在xml中控制節點名稱?

然而看到節點名稱很可怕。我想知道除了更改文件名之外,它們是否可以更改?我嘗試過xmlRoot,但沒有做任何事情。

比如我不喜歡這樣

return Request.CreateResponse<ResponseResult<List<PersonSearchDto>>>(HttpStatusCode.OK, p); 

回我得到一個名爲像這樣的根節點

ResponseResultOfArrayOfPersonSearchK0AojvId 

將是很好,如果我可以重命名爲類似Persons

回答

0

添加此行在您的Global.asax.cs

GlobalConfiguration.Configuration.Formatters.XmlDataContractSerializer = true; 

然後裝飾使用[DataMember][DataContract]類和屬性,如this MSDN article解釋說:

namespace MyTypes 
{ 
    [DataContract] 
    public class PurchaseOrder 
    { 
     private int poId_value; 

     // Apply the DataMemberAttribute to the property. 
     [DataMember] 
     public int PurchaseOrderId 
     { 

      get { return poId_value; } 
      set { poId_value = value; } 
     } 
    } 
} 
+0

爲什麼downvote? – 2013-04-21 01:58:04

+0

不知道爲什麼你被拒絕了......智能不會給我輸入的命名空間。這僅適用於wcf嗎?我正在使用asp.net web api。 – chobo2 2013-04-21 02:37:24

+0

適用於Web API ...您需要添加對「System.Runtime.Serialization.dll」的引用。 – 2013-04-21 02:44:32

相關問題