如何解析从文件加载的soap消息?

Ant*_*ère 16 c# xml wcf soap web-services

我需要解析从磁盘加载的SOAP消息到生成的代理的类型.WCF在从http服务器接收消息时执行此操作,因此我应该可以从磁盘执行此操作.

我使用WCF使用Web服务,我从远程WSDL生成代理客户端.

这是我从网络收到的XML结构(它是用System.ServiceModel.MessageLogging记录的),我想解析生成的类CRResponse:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:PourtuIntf" xmlns:ns2="ns2:PourtuIntf-IPourtu">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <ns2:GetCRResponse>
         <return>
            <ResultCode>0</ResultCode>
            <CR>
               <Theme SOAP-ENC:arrayType="ns1:ItemType[5]">
                  <item>
                     <Key/>
                     <Section SOAP-ENC:arrayType="ns1:Section[3]">
...
Run Code Online (Sandbox Code Playgroud)

当我调用Web服务的'GetCR'操作时,消息被正确转换为WCF生成的代理客户端类型GetCRResponse,但我不知道WCF是如何工作的,我需要从磁盘解析文件.

我试图以这种方式解析消息:

  GetCRResponse body;
  using (var xmlReader =  XmlReader.Create("Requests\\CR.xml"))
  {
      Message m = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap11);
      body = m.GetBody<GetCRResponse>();
  }
Run Code Online (Sandbox Code Playgroud)

在GeyBody方法中引发了这个异常:

Expected element 'ActGetCRResponse' from namespace 'http://schemas.datacontract.org/2004/07/Pourtu.PourtuClient'.. Detecting 'Element' with name 'ActGetCRResponse', namespace 'urn:PourtuIntf-IPourtu'.

我尝试使用SoapFormatter:

using ( FileStream fs = new FileStream("Requests\\CR.xml", FileMode.Open) )
{
    SoapFormatter formatter = new SoapFormatter();
    body = (ActGetCRResponse)formatter.Deserialize(fs);
}
Run Code Online (Sandbox Code Playgroud)

.. Deserialize抛出以下异常:分析错误,没有与xml键'ns2 GetCRResponse'关联的程序集.

我不能使用xml序列化程序反序列化为GetCRResponse,因为SOAP-ENC:arrayType需要由soap序列化程序解释.

Pop*_*opo 1

更新:

Message m = Message.CreateMessage(XmlReader.Create("C:\\testSvc\\login.xml"), int.MaxValue, MessageVersion.Soap11);
            SoapReflectionImporter importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:PlotiIntf-IPloti");
            XmlTypeMapping mapp = importer.ImportTypeMapping(typeof(ActGetCRResponse));
            XmlSerializer xmlSerializer = new XmlSerializer(mapp); 
            var o = (ActGetCRResponse)xmlSerializer.Deserialize(m.GetReaderAtBodyContents());
Run Code Online (Sandbox Code Playgroud)

参考