I am trying to make a soap call and deserialize the answer I receive back. The soap call is made correctly but I don't manage so far to deserialize the answer into an object. Specifically speaking, the XML contained in the answer is an array of repeated data. As you can guess I want to create an array of objects out of it. I have checked other similar questions but so far none worked. Let's start by defining the whole XML.
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">ThisIsATry/RetrieveResponse</a:Action>
</s:Header>
<s:Body>
<RetrieveResponse xmlns="ThisIsATry">
<RetrieveInsurersResult xmlns:b="http://schemas.datacontract.org/2004/07/ThisIsATry" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:Errors xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
<b:Message>Selected 2 records</b:Message>
<b:Results xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<c:ArrayOfKeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>PersonId</c:Key>
<c:Value>1</c:Value>
</c:KeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>Name</c:Key>
<c:Value>Mike</c:Value>
</c:KeyValueOfstringstring>
</c:ArrayOfKeyValueOfstringstring>
<c:ArrayOfKeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>PersonId</c:Key>
<c:Value>2</c:Value>
</c:KeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>Name</c:Key>
<c:Value>Henry</c:Value>
</c:KeyValueOfstringstring>
</c:ArrayOfKeyValueOfstringstring>
</b:Results>
<b:Warnings xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
</RetrieveInsurersResult>
</RetrieveResponse>
</s:Body>
</s:Envelope>
The part I need to use is:
<b:Results xmlns:c="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<c:ArrayOfKeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>PersonId</c:Key>
<c:Value>1</c:Value>
</c:KeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>Name</c:Key>
<c:Value>Mike</c:Value>
</c:KeyValueOfstringstring>
</c:ArrayOfKeyValueOfstringstring>
<c:ArrayOfKeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>PersonId</c:Key>
<c:Value>2</c:Value>
</c:KeyValueOfstringstring>
<c:KeyValueOfstringstring>
<c:Key>Name</c:Key>
<c:Value>Henry</c:Value>
</c:KeyValueOfstringstring>
</c:ArrayOfKeyValueOfstringstring>
</b:Results>
As you can see there are 2 "objects" of type c:ArrayOfKeyValueOfstringstring>. Each object contains 2 properties of type c:KeyValueOfstringstring . Finally, each one of these properties contains a key and a value . What I need is an array of c:ArrayOfKeyValueOfstringstring, containing an array of c:KeyValueOfstringstring and the relative informations. I tried to represent this data in my c# code with the following classes:
public class ArrayOfKeyValueOfstringstring
{
[XmlElement("ArrayOfKeyValueOfstringstring")]
public KeyValueOfstringstring[] Value { get; set; }
}
public class KeyValueOfstringstring
{
[XmlElement("KeyValueOfstringstring")]
public KeyValue Pair { get; set; }
}
public class KeyValue
{
[XmlElement("Key")]
public string Key { get; set; }
[XmlElement("Value")]
public string Value { get; set; }
}
The way I deal with the response so far is:
var result = client.UploadString(dataBaseConnectionString, soapTemplate); //calls the serive and brings back the XML
var document = XDocument.Parse(result); //gives back the first XML I posted in this question
var results = document.Descendants().FirstOrDefault(x => x.Name.LocalName == "Results"); //gives back the 2nd XML I posted
xmlNamespaceManager.AddNamespace("c", "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
var oXmlSerializer = new XmlSerializer(typeof(SoapResponse[]));
//this part is wrong..
using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(results.ToString())))
{
var responseObj = (ArrayOfKeyValueOfstringstring)oXmlSerializer.Deserialize(mem);
}
Thanks in advance for the help!