0

SCENARIO

I am implementing an interface layer between my company platform and a partner set of web services. These services are in XML but not SOAP an are exposed via https.

Each service has got a XSD file from where I have created the related class.

The partner has got a production environment and a sanbox environment. The namespaces in the XSD files exclusivelly refer to the production environment. So all the classes created using the xsd tool and the XSD files has got attributes referring to the production environment:

[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://url/to/production/environment/getnewsalesresult")]

ISSUE

Now I am testing the business logic in the sandbox environment. The web service XML response refers to the sandbox namespace:

xmlns="http://url/to/sandbox/environment/getnewsalesresult"

Eventually, when deserializing the response into the corresponding class I get this error:

There is an error in XML document (5, 2). ---> System.InvalidOperationException: <getnewsalesresult xmlns='http://url/to/sandbox/environment/getnewsalesresult'> was not expected.

Because the target class does not know the sandbox namespaces!!

PROOF

I have created a fake XML response based on a sandbox response and replacing the sandbox namespace references with the production namespace references. I successfully deserialized this response into the corresponding class.

QUESTION

Is it possible to programmatically set the value of the namespace in the XmlTypeAttribute attribute:

[System.Xml.Serialization.XmlTypeAttribute(Namespace = myProgrammaticallySetNamespaceValue)]
1
  • You're fixing the wrong problem - if the namespaces on the XML that the partner is returning vary depending on which environment produces them, then that's a logical error at their end. They don't understand XML. Jul 22, 2014 at 7:53

2 Answers 2

0

One way to "fix" this issue would be to make copies of the XSD file and edit them for each environment that you'll need to talk to. Then generate a set of classes from each XSD and place them in separate assemblies (or, at least, within separate namespaces in a single assembly).

Then, at runtime, you'd choose which set of classes to actually work with based on the current environment. You'd probably benefit from using partial classes to introduce a single interface (or a base class) that each of these implementations derives from, so that the rest of your code can work with them consistently.

Of course, the saner fix is to get your partner to fix their XML generation. If, logically, two pieces of XML represent the same "type" of objects, then they should be described by a single schema which has a single namespace. The namespaces applied to the XML should not depend on the current URL at which the XML is being retrieved (indeed, in general, namespaces need not be URLs at all, but merely URIs). But this is a "political" issue that may not be in your power to fix at this time.

1
  • Thanks Damien. I have thought about this solution, i.e. to create custom classes referring the sandbox namespace. But I should also create the logic to dinamically refer an assembly or another at run-time. I would avoid this. Jul 22, 2014 at 8:07
0

Eventually I decided to programmatically replace the references to the sandbox feturing in the http response with the references to the production environment.

I do not like much this solution, but actually I had to deploy the project:

    /*
     * This method is generic so I can perform request to different webservices
     * The HttpRequestConfiguration and the HttpRequestManager are two classes we use to wrap low level http request/response process.
     */
    internal T PerformHttpRequest<T>(HttpRequestConfiguration httpConf, bool overrideNS = false, string oldNS = null, string newNS = null)
    {

        T result = default(T);
        try
        {
            string xmlContent = HttpRequestManager.PerformHttpRequest<string>(httpConf);

            /*
             * override the namespace, replacing the oldNS with the newNS
             * not the best solution
             */
            if (overrideNS && !string.IsNullOrEmpty(oldNS) && !string.IsNullOrEmpty(newNS))
                xmlContent = xmlContent.Replace(oldNS, newNS);

            /*
             * deserialize the content into a T object
             */
            var serializer = new XmlSerializer(typeof(T));
            using (TextReader reader = new StringReader(xmlContent))
            {
                result = (T)serializer.Deserialize(reader);
            }
        }
        catch (Exception ex)
        {
            /* manage the exception */
        }

        return result;

    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.