2

There are two versions of a legacy web service I've inherited, I only have code for the latest version and need to make it backwards compatible.

The following XML works correctly with this service (and this is how most 3rd party clients send data):

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetServiceOrderByID xmlns="http://www.xxxxxxxx.co.uk">
      <request>
        <CurrencyCode xmlns="http://xxxxxxxx.co.uk/">GBP</CurrencyCode>
        ...

However when it receives the below XML from another 3rd party client that does not have name spaces on the XML elements (they will not change their request) the object de-serialises without an exception, but doesn't have any values in any of the properties.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetServiceOrderByID xmlns="http://www.xxxxxxxx.co.uk">
      <request>
        <CurrencyCode>GBP</CurrencyCode>

I can make one or the other work by adding the following attribute to the class that I'm de-serialising

<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://xxxxxxxx.co.uk/")>

But I need the service to be able to accept and deserialise both requests (note there are other web methods with other request objects that also have this problem)

I know I can get hold of the input stream and get the XML this way, but is there a way to tell the framework to ignore the namespaces, or a way to solve this without parsing the Xml text.

Mr Shoubs
  • 14,629
  • 17
  • 68
  • 107
  • 1
    Can you show how you read this `XML` and deserialize it? – Trevor Mar 23 '16 at 18:33
  • The .net framework takes care of it - standard webservice so when it reaches the webmethod it is already de-serialised. So however this does it - I don't do anything special. – Mr Shoubs Mar 23 '16 at 22:07
  • My bad, dumb moment. I forgot it's a web service... – Trevor Mar 23 '16 at 22:25
  • 1
    The serializer doesn't know about the `Xml` namespace because the `.net` type doesn't declare it. You are correct about adding the attribute to the class as well which is a way to do it, but doesn't help when you do have the namespace. The only other way I know of is to create a helper function that takes the `Xml` and then use `regex` to strip/parse the namespaces and return the `Xml` back... Or just ask the client to include the namespaces :) (***usually doesn't work to well***)... Good luck! – Trevor Mar 30 '16 at 15:52
  • @Codexer I thought this may be the case. I can see I can get the request from the current context, if you could answer how to do this with the regex code in an answer, I'll try it and then assign the points - they are sending code without the namespaces, so I need to know the best way to add them in so I can de-serialise using the standard XML de-serialise (and you are correct, they will not change what they send). – Mr Shoubs Mar 30 '16 at 16:39
  • 1
    Are you using or could you use WCF? WCF gives you extension points at different levels so you could use an IDispatchMessageInspector or IDispatchMessageFormatter to prepare an incoming request. – nodots Mar 31 '16 at 13:03
  • @nodots unfortunately this is a legacy web service and ideally I'd rather avoid recreating the whole things as WCF, its quite a mess (and in a previous version of .Net that uses DLLs tied to 2.0). We can just have a second version running and leave it there as this requires much less development time, but obviously isn't the ideal solution. Can you give an example of IDispatchMessageFormatter in an answer and I'll see how much effort it involves. – Mr Shoubs Mar 31 '16 at 15:40
  • 2
    @MrShoubs, this link may be useful for you. http://stackoverflow.com/questions/870293/can-i-make-xmlserializer-ignore-the-namespace-on-deserialization – Karthick Gunasekaran Apr 01 '16 at 09:53

0 Answers0