how to ignore soap stuff on deserializing xml to object?

后端 未结 1 603
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 15:01

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 XmlDocum         


        
相关标签:
1条回答
  • 2020-12-10 15:32

    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.
    }
    
    0 讨论(0)
提交回复
热议问题