如何忽略反序列化xml到对象的肥皂的东西? [英] how to ignore soap stuff on deserializing xml to object?

查看:99
本文介绍了如何忽略反序列化xml到对象的肥皂的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取xml时,需要将其反序列化为特定对象,然后通过Web服务方法中的参数将其传递。

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

代码:

 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.

我需要忽略信封标记和其他SOAP内容。
我该怎么做?

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

xml文件:

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

测试代码:

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

肥皂身体获得< ns:INCLUIRCONHECIMENTOFRETESAIDA> 以及我需要的所有信息。但是,当我将其反序列化为 responseObj 时,所有值都为null。

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.

推荐答案

我没有足够的详细信息来为您填写名称空间和元素名称,但是使用了 W3C示例SOAP响应,下面的代码和类反序列化对象:

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

您可以对 OCTE 类。

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

这篇关于如何忽略反序列化xml到对象的肥皂的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆