My C# class:
[XmlRoot("ResultDetails")]
public class MyClass
{
[XmlElement("Id")]
public string Id { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
}
I'm receiving the XML from a SOAP request, so the soap body looks like this:
<soap:Body>
<Response xmlns="http://services.serviceprovider.co.uk/">
<Result>
<ResultDetails>
<Id>3636346</Id>
<Name>MyName</Name>
</ResultDetails>
</Result>
</Response>
</soap:Body>
And trying to deserialize it like this:
var xmlResponse = XDocument.Load(soapResult);
var myClassRootElement = (from xElement in xmlResponse.Descendants()
where xElement.Name.LocalName == "ResultDetails"
select xElement).First();
var serializer = new XmlSerializer(typeof(MyClass));
var myClass= (MyClass)serializer.Deserialize(myClassRootElement.CreateReader());
The last line is throwing the following exception:
System.InvalidOperationException: There is an error in XML document (0, 0). ---> System.InvalidOperationException: <ResultDetails xmlns="http://services.serviceprovider.co.uk/"> was not expected.
at Microsoft.Xml.Serialization.GeneratedAssembly
Which I don't understand why, because surely as I've set ResultDetails as the XmlRoot of MyClass it should be exactly what the deserializer was expecting?