Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

c# - how to ignore soap stuff on deserializing xml to object?

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:DesktopCteWebservice.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:DesktopCteWebservice.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.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...