0

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;
        
4
  • Does it work any better if you call CreateReader on the XElement to get an XmlReader and get rid of the intermediate messing about with streams? It does appear to be a fragment with undeclared prefixes. Commented Mar 3, 2022 at 11:37
  • Hi, @Damien_The_Unbeliever. When I use that, I'm getting the Base64 data type error. One of the properties of my object has that data type that's why CreateReader is not working for that specific object. Commented Mar 3, 2022 at 11:58
  • 1
    Then it's highly likely that if/when you get past this issue you're creating by only serializing the fragment without necessary namespaces you'll get right back to that same base64 error. Fix that problem. Commented Mar 3, 2022 at 13:22
  • Can you provide a minimal reproducible example? You state the error says the xsd prefix is missing, but from the sample it looks like the xsi prefix is missing. It also seems, as @Damien_The_Unbeliever suggests, the actual issue relates to this 'base64' data, but this cannot be reproduced from your sample XML and code. Commented Mar 3, 2022 at 14:06

2 Answers 2

1

I think the XML is missing this line

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

Sign up to request clarification or add additional context in comments.

1 Comment

Hi! Yes, it is declared on the parent node. However, I only need the specific xml element to deserialize. I edited the above description for clarity.
0

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">

2 Comments

Hi! I don't have the control what the XML might look like, but should I just manually add it via string manipulation or XML atrribute (not sure what to use though), I'm new to this.
You're getting bad XML from some service. Don't try to repair it, get the problem fixed at source. You can't build a good system on top of bad data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.