I am trying to deserialize an XML element that I received from a SOAP response, but I am getting the "Namespace prefix 'xsd' is not defined" error. What's the possible solution for this?
Here's the sample response XML I'm getting:
<ns0:response xmlns:ns0="http://sample.site.com">
<ns0:additionalData>
<ns0:entry>
<ns0:key xsi:type="xsd:string">Test Key</ns0:key>
<ns0:value xsi:type="xsd:string">1</ns0:value>
</ns0:entry>
<ns0:entry>
<ns0:key xsi:type="xsd:string">Test Code</ns0:key>
<ns0:value xsi:type="xsd:string">1</ns0:value>
</ns0:entry>
</ns0:additionalData>
<ns0:mpiData>
<ns1:authenticationResponse xmlns:ns1="http://sample.site2.com">Y</ns1:authenticationResponse>
</ns0:mpiData>
</ns0:response>
I'm deserializing it this way:
XDocument xDocument = XDocument.Parse(responseText);
var responseElement = xDocument.Descendants().Where(a => a.Name.LocalName == response.ElementName).Select(a => a).Last();
TResponse response = default(TResponse);
Stream memoryStream = new MemoryStream();
responseElement.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
var serializer = new XmlSerializer(typeof(TResponse));
using (XmlReader xmlReader = XmlReader.Create(memoryStream))
{
response = (TResponse)serializer.Deserialize(xmlReader);
}
return response;
CodePudding user response:
I think the XML is missing this line
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
CodePudding user response:
Declare the xsd
namespace prefix by adding
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
at or above the element on which it's used.
For example, add it to the ns0:response
element, which is common to all likely places it will be needed:
<ns0:response xmlns:ns0="http://sample.site.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">