10

When I get a xml, I need to deserialize it to a specific object and pass it via parameter in a web service method.

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));

 serviceClient.InsertOCTE(octe);

But when I try to deserialize I get a error saying

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" > was not expected.

I need to ignore the envelope tag and other SOAP stuff. How can I do that?

The xml file:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
              xmlns:ns="http://192.168.1.180:8085/">
 <soapenv:Header/>
 <soapenv:Body>
  <ns:INCLUIRCONHECIMENTOFRETESAIDA>
     <ns:CONHECIMENTOFRETE>
        <ns:CSTICMS></ns:CSTICMS>
     </ns:CONHECIMENTOFRETE>
  </ns:INCLUIRCONHECIMENTOFRETESAIDA>
<soapenv:Body>

Test Code:

XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace m = "http://192.168.1.180:8085/"; 
var soapBody = xdoc.Descendants(soap + "Body").First().FirstNode;

var serializer =  new XmlSerializer(typeof(OCTE));
var responseObj = (OCTE)serializer.Deserialize(soapBody.CreateReader());

The soap Body gets the <ns:INCLUIRCONHECIMENTOFRETESAIDA> with all information that I need. But when I deserialize it to responseObj I get all values as null.

Boiethios
  • 38,438
  • 19
  • 134
  • 183
gog
  • 11,788
  • 23
  • 67
  • 129
  • Use `XDocument` and do `document.Descendant("OCTE")`? (or whatever the root name should be) – Tim S. Dec 09 '13 at 18:48
  • Descendant, not sure if i can find this member on XmlDocument class. – gog Dec 09 '13 at 18:51
  • right, I confused it with `XDocument`. Can you use that class instead? Either way though, I'm sure there's a way to navigate the XML tree down to your body's value. – Tim S. Dec 09 '13 at 18:54
  • I can use, but then with Descendant i get an IENumerable instead a OCTE object – gog Dec 09 '13 at 19:24
  • possible duplicate of [removing/extracting soap header and body from soap message](http://stackoverflow.com/questions/20270314/removing-extracting-soap-header-and-body-from-soap-message) – Mauricio Gracia Gutierrez Dec 09 '13 at 19:46
  • Why not just deserialize with [`SoapFormatter`](http://msdn.microsoft.com/en-us/library/wkyt1t1f(v=vs.110).aspx) instead? – D Stanley Dec 10 '13 at 19:37

1 Answers1

8

I don't have enough details to fill in the namespaces and element names for you, but using W3C's example SOAP response, the following code and classes deserialize the object:

var xdoc = XDocument.Load(@"C:\Desktop\CteWebservice.xml");
XNamespace soap = "http://www.w3.org/2001/12/soap-envelope";
XNamespace m = "http://www.example.org/stock";
var responseXml = xdoc.Element(soap + "Envelope").Element(soap + "Body")
                      .Element(m + "GetStockPriceResponse");

var serializer = new XmlSerializer(typeof(GetStockPriceResponse));
var responseObj =
      (GetStockPriceResponse)serializer.Deserialize(responseXml.CreateReader());


[XmlRoot("GetStockPriceResponse", Namespace="http://www.example.org/stock")]
public class GetStockPriceResponse
{
    public decimal Price { get; set; }
}

You could do the same with your OCTE class.

[XmlRoot("INCLUIRCONHECIMENTOFRETESAIDA",Namespace="http://192.168.1.180:8085/")]
public class OCTE
{
    // with property mapping to CONHECIMENTOFRETE, etc.
}
brendonofficial
  • 850
  • 8
  • 10
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • Sorry but i dont get what is that response. The webservice doesnt return any response. I put some important part of my xml in the OP. thanks – gog Dec 10 '13 at 19:18
  • @ggui the response corresponds to whatever class you have that mirrors INCLUIRCONHECIMENTOFRETESAIDA. I'm assuming this is OCTE, and have added code that shows what sort of attribute you'd need to put on that for the serializer to understand that that's what it is. – Tim S. Dec 10 '13 at 19:45
  • Yes, it is the OCTE Class. I get the information of the octe.xml file, then desiarize it to a OCTE object then send to the webservice as parameter. Ill try your solution. Thanks! – gog Dec 11 '13 at 11:22
  • ok i get it. now i can get just the "important" part of the xml for my OCTE object. But when i deserialize it, i get all values null on the responseObj. – gog Dec 11 '13 at 12:52
  • @ggui you probably need to include `XmlElementAttribute`s on your properties with the name and namespace (so that the serializer knows how to map the XML to your properties). – Tim S. Dec 11 '13 at 13:03