2011-05-20 27 views
0

當前XML輸出如下:WCF datacontractserialization - 意想不到的元素<a:anyType>

<response xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <totalResultCount>10</totalResultCount> 
<results xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
<a:anyType i:type="result"> 
    <EmployeeCode>007</EmployeeCode> 
    <EmployeeName>Bond, James</EmployeeName> 
</a:anyType> 
<a:anyType i:type="result"> 
    <EmployeeCode>006</EmployeeCode> 
    <EmployeeName>Foo, Bar</EmployeeName> 
    </a:anyType> 
</results> 
</response> 

我想在XML是格式爲:

 <response xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <totalResultCount>10</totalResultCount> 
    <results xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> 
    <result> 
     <EmployeeCode></EmployeeCode> 
     <EmployeeName></EmployeeName> 
     </result> 
</results> 
</response> 

數據契約

internal static class KnownTypesProvider 
     {  
      public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)  
      { 
       // collect and pass back the list of known types  
       List<Type> types = new List<Type>(); 
       types.Add(typeof(EmployeeDTO)); 
       return types; 
      } 
     } 

     [DataContract(Name = "response")] 
     public class Response 
     { 
      [DataMember(Order = 1)] 
      public int totalResultCount { get; set; } 
      [DataMember(Order = 2)]   
      public IEnumerable results { get; set; } 
     } 

     [DataContract(Name = "result")] 
     public class EmployeeDTO 
     { 
      [DataMember] 
      public string EmployeeCode { get; set; } 
      [DataMember] 
      public string EmployeeName { get; set; } 
} 

我是什麼missi在這裏?

回答

1

IEnumerable是Object的列表,因此WCF正在將該類型添加到輸出中。您可以使用IEnumerable <EmployeeDTO>或列表<EmployeeDTO>?

相關問題