I want to save the response I receive when sending a request as an object. This is my code:
[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:
<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.