Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to save the response I receive when sending a request as an object. This is my code:
C#
[XmlRoot("GetStateOwnedResponse")]
public class GetEmployeeResponse 
 {
    PropertyStatus prStatus;
 }

 public class PropertyStatus
 {
    List<Employee> employee;
 }

public class Employee 
 {
    [XmlElement("id")] 
     int id;
     [XmlElement("name")] 
     string name;
 }

private void reqStateOwned(Employee empl)
 {

      var client = new RestClient(ConfigurationManager.AppSettings["base_url"] + "/" +
                                        ConfigurationManager.AppSettings["provider"] + "/" +
                                        ConfigurationManager.AppSettings["serviceName"])
     {
         Timeout = -1
      };

      var request = new RestRequest(Method.POST);

      var authenticationString = string.Format("{0}:{1}", ConfigurationManager.AppSettings["serviceUser"], ConfigurationManager.AppSettings["servicePass"]);
      var base64EncodedAuthenticationString = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

      request.AddHeader("Authorization", "Basic " + base64EncodedAuthenticationString);
      request.AddHeader("Content-Type", "text/plain");

      var body = @"<GetEmployee xmlns=""http://test.dev.com/"">"
                        + "\n" + @"    <Id_No>" + empl.idno + "</Id_No>"
                        + "\n" + @"</GetEmployee>";


     request.AddParameter("text/plain", body, ParameterType.RequestBody);

     IRestResponse response = client.Execute(request);

     System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(GetEmployeeResponse), new XmlRootAttribute("GetEmployeeResponse"));
    StringReader stringReader = new StringReader(response);
    GetEmployeeResponse list = 
 (GetEmployeeResponse)serializer.Deserialize(stringReader));

    foreach (Employee item in list.prStatus.Items)
       System.Console.WriteLine(item.Property_Description));

}


and this is the XML response I get:
XML
<GetEmployeeResponse xmlns="http://test.dev.com/">
    <PropertyStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <Employee>
            <Name>John</Name>
            <Id>6584</Id>
        </Employee>
        <Employee>
            <Name>Chris</Name>
            <Id>4120</Id>
        </Employee>
    </PropertyStatus>
</GetEmployeeResponse>


I get this error: System.InvalidOperationException: <getemployeeresponse xmlns="http://test.dev.com/"> was not expected.

How can I read the above XML file?

Any ideas what am I doing wrong?

Thank you in advance.

What I have tried:

Ignore <getemployeeresponse> tag and use <propertystatus> as root element but I get the same error.
Posted
Updated 1-Sep-21 2:13am

1 solution

You need to pass the correct namespace to the XmlRootAttribute:
XmlRootAttribute Class (System.Xml.Serialization) | Microsoft Docs[^]
C#
XmlSerializer serializer = new XmlSerializer(typeof(GetEmployeeResponse), new XmlRootAttribute("GetEmployeeResponse") { Namespace = "http://test.dev.com/" });
 
Share this answer
 
Comments
Chriz12 2-Sep-21 3:59am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900