2014-01-07 91 views
0

我使用的是Asp.net Web Api,我試圖返回一個DTO對象列表。Asp.net Web API序列化列表<>

這是我有:

// Wrapper 
[DataContract(Namespace = "", Name = "get-applications-result")] 
[XmlRoot(Namespace = "", ElementName = "get-applications-result")] 
public class GetApplicationsResult : List<ApplicationDTO> 
{ 

} 

// DTO object 
[DataContract(Namespace = "", Name = "application")] 
[XmlRoot(Namespace = "", ElementName = "application")] 
public class ApplicationDTO 
{ 
    [DataMember(Name = "application-name")] 
    [XmlElement(ElementName = "application-name")] 
    public string ApplicationName { get; set; } 
} 

Global.asax我設置

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; 

然而,當我返回從服務器到客戶端的響應,它返回它是這樣的:

<get-applications-result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <ApplicationDTO> 
     <application-name>Application name</application-name> 
    </ApplicationDTO> 
</get-applications-result> 

我怎樣才能使<ApplicationDTO/>被渲染爲<application/>

回答

0

嘗試在ApplicationDTO類而不是XmlRootAttribute使用XmlTypeAttribute

[DataContract(Namespace = "", Name = "application")] 
[XmlType(TypeName = "application")] 
public class ApplicationDTO 
{ 
    [DataMember(Name = "application-name")] 
    [XmlElement(ElementName = "application-name")] 
    public string ApplicationName { get; set; } 
}