XML : How to deserialise a class without a namespace or with one that isn't expected

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 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 I a way to solve this without parsing through the Xml text.

No comments:

Post a Comment