Data is returned from an Apache web service, so I am unable to use the Add Service Reference
as the generated classes aren't able to make a web service call or return data
I am trying to deserialize the below SOAP response in C# .NET. I can validate that the XDocument
statement correctly pulls just the actual response I want to deserialize (taken from this answer), however when I inspect the response object, both Attribute1
and the MapItem
list are null
.
Any help would be greatly appreciated.
SOAP Response
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:checkcompanyResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
<checkcompanyReturn xsi:type="ns2:Map" xmlns:ns2="http://xml.apache.org/xml-soap">
<item xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<key xsi:type="soapenc:string">Entry1</key>
<value xsi:type="soapenc:string">Y</value>
</item>
<item>
<key xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">Entry2</key>
<value xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">N</value>
</item>
</checkcompanyReturn>
</ns1:checkcompanyResponse>
</soapenv:Body>
</soapenv:Envelope>
C# Classes
[XmlRoot(ElementName = "checkcompanyResponse", Namespace = "http://DefaultNamespace")]
public class CheckCompanyResponse {
[XmlElement("attribute1")]
public string Attribute1 { get; set; }
[XmlArray("checkcompanyReturn")]
[XmlArrayItem("item")]
public List<MapItem> Map { get; set; }
}
public class MapItem {
public string key { get; set; }
public string value { get; set; }
}
Deserialization code
string fileName = @"C:\Users\SLUKY\Desktop\test.xml";
XDocument document = XDocument.Load(fileName);
XName soapBody = XName.Get("Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(CheckCompanyResponse));
CheckCompanyResponse response = (CheckCompanyResponse)xmlSerializer.Deserialize(document.Descendants(soapBody)
.First()
.FirstNode
.CreateReader()
);