2013-04-21 78 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