locked
adding namespaces to XmlSerializer when deserializing (C#)

    Question

  •  

    Hi there
    I'm hoping there's an easy answer to a (hopefully) not too long-winded issue...
    I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different namespace. Some schemas extend complex types in others. When i get a soap:Fault from my test server it could contain a serialized exception object (called fault) from one of two of these schemas (and different namespaces, call them xmlns:b0="ns1" and xmlns:eb="ns2"), yet the root element of the SoapException.Detail node will remain the same (in "ns1"). The prefix declaration is in the soap:Fault node of the response.

     

    Now, if the object i'm trying to desrialize is defined in the same schema as the root element <b0:faultResp> (e.g. <b0:fault xsi:type="b0:ThisEx">) i create an XmlSerializer with this default namespace and it deserializes no problem, and the root element of the Detail node becomes <b0:faultResp xmlns:b0="ns1">. If it's defined in the other schema (e.g. <b0:fault xsi:type="eb:ThatEx">). i get an error: "Namespace prefix 'eb' is not declared", yet both prefixes are defined in the <soap:Fault xmlns:b0="ns1" xmlns:eb="ns2"> node

    I understand that XmlSerializer.Serialize method takes an XmlNamespaces parameter which allows you to add any other namespaces it needs when serializing, but XmlSerializer.Desrialize doesn't; i can't find any way to at all. SoapFormatter.deserialize didn't work nor did using XmlSerializerFactory to create the serializer. So my questions then:

    1. Is there some way to specify additional namespaces (like that  xmlns:eb="ns2" guy) to the XmlSerializer object before deserializing?
    2. Is there a way to extract the namespace declarations in the soap:Fault node from the SoapException the webmethod catches?
    3. Is there a way of doing this without worrying about the namespaces at all?

     

    The Fiddler'd response looks like this:
    <?xml version="1.0"?>
    <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
     <Body>
      <soap:Fault xmlns:b0="ns1" xmlns:eb="ns2" xmlnsTongue Tiedoap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <faultcode xmlns="">soapTongue Tiederver</faultcode>
      <faultstring xmlns="">An exception was thrown on the server/faultstring>
       <detail xmlns="">
        <b0:faultResp>
         <b0:fault xsi:type="eb:ThatEx">
          <b0Big Smileesc>That other exception occurred</b0Big Smileesc>
         </b0:fault>
        </b0:faultResp>
       </detail>
      </soap:Fault>
     </Body>
    </Envelope>


    Excerpt from schema 1 (xmlns:b0="ns1")

    <element name="faultResp" type="i0:FaultResp">
     <annotation>
      <documentation>A fault.</documentation>
     </annotation>
    </element>
    <complexType name="faultResp">
     <annotation>
      <documentation>A fault.</documentation>
     </annotation>
     <complexContent>
      <extension base="i0:BaseResp">
       <sequence>
        <element minOccurs="1" maxOccurs="1" name="fault" type="i0:Fault"/>
       </sequence>
      </extension>
     </complexContent>
    </complexType>
    ...
    <complexType name="Fault" abstract="true">
     <annotation>
      <documentation>A generic fault.</documentation>
     </annotation>
     <sequence>
      <element minOccurs="1" maxOccurs="1" name="desc" type="i0:Msg">
       <annotation>
        <documentation>description of the fault</documentation>
       </annotation>
      </element>
     </sequence>
    </complexType>

    Excerpt from schema 2 (xmlns:eb="ns2")

    <complexType name="ThatEx">
     <annotation>
      <documentation>That Other exception.</documentation>
     </annotation>  
     <complexContent>
      <extension base="b0:Fault" />
     </complexContent>
    </complexType>

    What I'm looking for is to deserialize the <b0:faultResp> into an object of type FaultResp, with a property of type ThatEx, that has a string property called desc.

     

    Any help appreciated.
    Dave

    Wednesday, November 21, 2007 12:10 PM

Answers

  • got it. turns out with enough patience, the right xml attributes and an XmlSerializerNamespaces class, serialization is probably the most configurable thing on earth.
    Wednesday, April 09, 2008 8:31 AM

All replies

  • got it. turns out with enough patience, the right xml attributes and an XmlSerializerNamespaces class, serialization is probably the most configurable thing on earth.
    Wednesday, April 09, 2008 8:31 AM
  • Could you provide more details on how you resolved it?  I'm running into problems deserializing xml that contains namespace prefixes.  Without the prefix I'm able to deserialize fine - I just can't figure out how to deserialize WITH the prefix. 

    ...running out of patience...

    Any help would be greatly appreciated!

    Tuesday, April 21, 2009 9:34 PM
  • You'll get better answers if you ask your own questions.
    John Saunders
    Use File->New Project to create Web Service Projects
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Friday, May 01, 2009 11:42 AM
  • That is a horrible answer, that negates the fact that the original poster came in with a question searching for proper answers. In the spirit of the community, he should post his resolution for anyone else that is a similar situation instead of being selfish. The community works both ways, giving and receiving. 
    Saturday, December 22, 2012 7:56 PM
  • I'm sorry, but I have no idea who you are responding to.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    Monday, December 24, 2012 5:31 PM