How to ignore soapy material when deserializing xml to an object?

When I get xml, I need to deserialize it to a special object and pass it through a parameter in the 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 an error

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

I need to ignore the envelope tag and other SOAP files. How can i do this?

Edit:

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 program receives all the information I need. But when I deserialize it to responseObj, I get all the values ​​as null.

+4
source share
2 answers

, , W3C SOAP, :

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

OCTE.

[XmlRoot("INCLUIRCONHECIMENTOFRETESAIDA",Namespace="http://192.168.1.180:8085/")]
public class OCTE
{
    // with property mapping to CONHECIMENTOFRETE, etc.
}
+6

, ,

/

+1

All Articles