WCF 数据契约序列化 - 意外元素

发布于 2024-11-08 07:41:38 字数 2055 浏览 11 评论 0原文

当前的 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">
 <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; }  
}

我在这里缺少什么?

The current xml output looks like:

 <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>

I would like the xml to be in this format:

     <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>

Data Contracts

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; }  
}

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-11-15 07:41:39

IEnumerable 是一个对象列表,因此 WCF 正在将类型添加到输出中。您可以使用 IEnumerable或列表

IEnumerable is a list of Object, so WCF is adding the type to the output. Can you use IEnumerable<EmployeeDTO> or List<EmployeeDTO>?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文