2
votes

I have a web application where the users can upload a specific XML file, then i need to convert the XML file to a object which is expect in a webservice.

Code:

var document = new XmlDocument();
document.Load(@"C:\Desktop\CteWebservice.xml");       

var serializer =new XmlSerializer(typeof(OCTE));
var octe = (OCTE)serializer.Deserialize(new StringReader(document.OuterXml));
var serviceClient = new WSServiceClient();
serviceClient.InsertOCTE(octe);

I get this error when i try to deserialize the xml document as a OCTE object:

< Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'> was not expect.

Obvious, the OCTE class doesn't have a Envelope property.

My question is, i need to include this tag in the OCTE class or i need to do something else in the client?

1

1 Answers

0
votes

I can think of at least two different solutions to your problem, which seems to be caused by the fact that the XML file contain the full SOAP request, which will contain a SOAP Envelope, a SOAP Body element and potentially a SOAP header element.

The structure of SOAP body element is dependent on the SOAP Binding style, which most likely will be Document Literal Wrapped - for further details look at http://www.ibm.com/developerworks/webservices/library/ws-usagewsdl/index.html?ca=dat-

This means that the OCTE structure that you are looking for, might be embedded inside a request element, something like (document literal wrapped SOAP binding style)

<soap:envelope>
    </soap:header>
    <soap:body>
        <insertOCTERequest>
            <OCTE>
                ...
            </OCTE>
        </insertOCTERequest>
    </soap:body>
</soap:envelope>

So to the possible solutions

  1. Ensure that the CteWebService.xml is serialised to have the OCTE structure as it's root element.

  2. Use etc. XPath to locate the OCTE element, and deserialise that instead.

    var nsmgr = ...
    var element = document.SelectSingleNode("//OCTE", nsmgr);
    

You only need to initialise a Xml NamespaceManager if the OCTE element belongs to a specified xml namespace, otherwise you can leave it out.