0

我正在使用 asp.net web api,尽管我个人喜欢使用 json 处理我的数据,HttpResponseMessageRequest.CreateResponse我可以让客户决定他们宁愿做什么。

但是,当查看 xml 数据时,我发现节点名称很糟糕。我想知道除了更改文件名之外可以更改它们吗?例如,我尝试了 xmlRoot,但什么也没做。

例如我做这样的回报

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

我得到一个这样命名的根节点

ResponseResultOfArrayOfPersonSearchK0AojvId

如果我可以重命名为类似Persons

4

2 回答 2

0

将此行添加到您的Global.asax.cs

GlobalConfiguration.Configuration.Formatters.XmlDataContractSerializer = true;

然后按照MSDN 文章中的说明使用[DataMember]和装饰您的类和属性:[DataContract]

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; }
        }
    }
}
于 2013-04-21T00:57:59.237 回答
0

解决问题的正确方法是使用 CollectionDataContractAttribute:

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.collectiondatacontractattribute.aspx

所以在你的情况下,你可以有这样的类型:

[CollectionDataContract(Name = "Persons")]
public class PersonResults : List<PersonSearchDto>
{
}
于 2013-04-23T01:15:17.243 回答