0

I contact a web service that returns something like 2600 records in SOAP format. To do some integration testing, I saved the soap message by copying the result from SoapUi to a file. I then removed most of the records (identified as Report_Entry) to give myself a much smaller test.

So, I'm writing a unit test that loads the file as XML:

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

(The WSDL defines the entries as Report_EntryType.)

Then:

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

But, my test fails on a

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

To summarize:

  1. Retrieve a SOAP message from a webservice.
  2. Save the message to a file.
  3. Remove most of the records from the test file so I only have one or two.
  4. Feed that file to a test case that uses XmlSerializer to give me input data.
  5. Use a string reader to read the input data.
  6. Blow up when trying to deserialize.

How do I solve the problem?

Sample file:

<?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>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
EoRaptor013
  • 1,735
  • 4
  • 18
  • 31
  • An Xml file is not well formed when you have an array of elements at the root. The deserialize method will fail. You root type is : List which is an array. You must wrap the array with a singular type. – jdweng Jul 16 '19 at 23:45
  • If you have a text file with multiple XML root elements one after the other, you can read it into a `List` as shown in [this answer](https://stackoverflow.com/a/46476652) to [Read nodes of a xml file in C#](https://stackoverflow.com/q/46476119/3744182). Does that answer your question? – dbc Jul 17 '19 at 07:27
  • dbc, your answer to that earlier question looks plausible for me, but I don't have multiple root entries. I was assuming (yes, I know what that means) the Envelope tag was root. The individual entries are tagged as Report_Entry. – EoRaptor013 Jul 17 '19 at 14:56
  • In that case can you please share a [mcve] that demonstrates how to reproduce the `System.InvalidOperationException` exception? The XML in your question is well-formed so there isn't an obvious problem. Or is your question, *How can I read though the XML file, find all elements with a specific namespace and name (here ``, and then deserialize that to a `List` where `T` represents my data model for that element*? – dbc Jul 17 '19 at 17:52
  • That's pretty much the question with the added little twist that the XML is a SOAP message saved to an xml file. Editing question to add the test method. – EoRaptor013 Jul 17 '19 at 19:13
  • For the moment, it looks like I can't save my unit test -- just the one method -- while editing my original question. I've asked for support, and will try again after that. The actual error message is System.InvalidOperationException HResult=0x80131509 Message=There is an error in XML document (2, 2). Source= StackTrace: Inner Exception 1: InvalidOperationException: was not expected. – EoRaptor013 Jul 17 '19 at 20:05
  • Well you could use something like `XmlExtensions.DeserializeElements(XmlReader reader, "Report_Entry", "urn:com.workday.report/wog_workday_to_ad_integration_report:2")` from [this answer](https://stackoverflow.com/a/49949869/3744182) to [Deserialize portion of XmlDocument into object](https://stackoverflow.com/q/49932706/3744182) to deserialize selected non-root elements in your XML document into a list, where the `XmlReader` streams directly from your XML file. Does that answer your question, or do you need an explicit answer? – dbc Jul 18 '19 at 08:17

0 Answers0