首页 > 解决方案 > 将文件中的 Soap 反序列化为 DTO

问题描述

我联系了一个以 SOAP 格式返回类似 2600 条记录的 Web 服务。为了进行一些集成测试,我通过将 SoapUi 中的结果复制到文件来保存了 soap 消息。然后我删除了大部分记录(标识为 Report_Entry)给自己一个小得多的测试。

所以,我正在编写一个将文件加载为 XML 的单元测试:

XmlSerializer deSerialize = new XmlSerializer(typeof(List<Report_EntryType>));

(WSDL 将条目定义为 Report_EntryType。)

然后:

{
    workdayList = deSerialize.Deserialize(reader) as List<Report_EntryType>;
}

但是,我的测试失败了

System.InvalidOperationException : <Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'> 

总结一下:

  1. 从 Web 服务检索 SOAP 消息。
  2. 将消息保存到文件中。
  3. 从测试文件中删除大部分记录,所以我只有一两条。
  4. 将该文件提供给使用 XmlSerializer 为我提供输入数据的测试用例。
  5. 使用字符串阅读器读取输入数据。
  6. 尝试反序列化时炸毁。

我该如何解决这个问题?

示例文件:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Body>
      <wd:report_data xmlns:wd="urn:com.workday.report/wog_workday_to_ad_integration_report:2">
         <wd:Report_Entry>
            <wd:Employee_ID>99995</wd:Employee_ID>
            <wd:Manager_ID>001961</wd:Manager_ID>
            <wd:Full_Legal_Name>Luke Skywalker</wd:Full_Legal_Name>
            <wd:Legal_Name_-_First_Name>Luke</wd:Legal_Name_-_First_Name>
            <wd:Legal_Name_-_Last_Name>Skywalker</wd:Legal_Name_-_Last_Name>
            <wd:Legal_Name_is_Preferred_Name>1</wd:Legal_Name_is_Preferred_Name>
            <wd:Preferred_Name_in_General_Display_Format>Luke Skywalker</wd:Preferred_Name_in_General_Display_Format>
            <wd:Preferred_Name_-_First_Name>Luke</wd:Preferred_Name_-_First_Name>
            <wd:Preferred_Name_-_Last_Name>Skywalker</wd:Preferred_Name_-_Last_Name>
            <wd:Hire_Date>2019-07-01-07:00</wd:Hire_Date>
            <wd:Original_Hire_Date>2019-07-01-07:00</wd:Original_Hire_Date>
            <wd:Job_Title_-_Current>Contractor</wd:Job_Title_-_Current>
            <wd:Position_Object>
               <wd:Current_Position_Filled_-_Effective_Date>2019-07-01-07:00</wd:Current_Position_Filled_-_Effective_Date>
            </wd:Position_Object>
            <wd:Email_-_Work wd:Descriptor="luke@whiting.com">
               <wd:ID wd:type="WID">106d0dc1e3f2017c123580628801d001</wd:ID>
            </wd:Email_-_Work>
            <wd:location wd:Descriptor="Mytown">
               <wd:ID wd:type="WID">d34191b0c905106f759d0975538e88c8</wd:ID>
               <wd:ID wd:type="Location_ID">LOC008-Myto</wd:ID>
            </wd:location>
            <wd:location_Object>
               <wd:Primary_Address_-_Line_1>701 4th Avenue NW</wd:Primary_Address_-_Line_1>
               <wd:Primary_Address_-_Line_2>PO Box 123</wd:Primary_Address_-_Line_2>
               <wd:Primary_Address_-_Line_3>Mytown, ND 12345</wd:Primary_Address_-_Line_3>
               <wd:city>Mytown</wd:city>
               <wd:state>North Dakota</wd:state>
               <wd:Primary_Address_-_Postal_Code>12345</wd:Primary_Address_-_Postal_Code>
            </wd:location_Object>
            <wd:Cost_Center_-_Name>Field Operations</wd:Cost_Center_-_Name>
            <wd:WOG_EE_Driver_Eligibility>0</wd:WOG_EE_Driver_Eligibility>
            <wd:Legal_Name_in_General_Display_Format>Luke Skywater</wd:Legal_Name_in_General_Display_Format>
            <wd:Cost_Center_-_ID>1010</wd:Cost_Center_-_ID>
            <wd:Worker_Status>Active</wd:Worker_Status>
            <wd:Location_Address_-_Region__Any_Country_ wd:Descriptor="North Dakota">
               <wd:ID wd:type="WID">0567a9c4e90340e192aa655d387323c6</wd:ID>
               <wd:ID wd:type="Country_Region_ID">USA-ND</wd:ID>
               <wd:ID wd:type="ISO_3166-2_Code">ND</wd:ID>
            </wd:Location_Address_-_Region__Any_Country_>
            <wd:Job_Code>CONTRACT</wd:Job_Code>
            <wd:Exempt>0</wd:Exempt>
            <wd:User_Name>lskywalker</wd:User_Name>
            <wd:Worker_is_Contingent_Worker>1</wd:Worker_is_Contingent_Worker>
            <wd:Contract_End_Date>2019-08-07-07:00</wd:Contract_End_Date>
            <wd:Cost_Center_Hierarchy wd:Descriptor="Operations">
               <wd:ID wd:type="WID">d9a3758225e01071585ef203864f2bfa</wd:ID>
               <wd:ID wd:type="Organization_Reference_ID">Operations</wd:ID>
               <wd:ID wd:type="Custom_Organization_Reference_ID">Operations</wd:ID>
            </wd:Cost_Center_Hierarchy>
            <wd:Region wd:Descriptor="Northern Rockies">
               <wd:ID wd:type="WID">d9a3758225e010715924f5d0bd472cde</wd:ID>
               <wd:ID wd:type="Organization_Reference_ID">Northern Rockies</wd:ID>
               <wd:ID wd:type="Region_Reference_ID">Northern Rockies</wd:ID>
            </wd:Region>
         </wd:Report_Entry>
      </wd:report_data>
   </env:Body>
</env:Envelope>

标签: c#xmlsoap

解决方案


推荐阅读