首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何解析从文件加载的soap消息?

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

Stack Overflow用户
提问于 2017-04-28 21:59:25
回答 1查看 1.3K关注 0票数 16

我需要将从磁盘加载的SOAP消息解析为生成的代理的类型。WCF在接收到来自http服务器的消息时执行此操作,因此我应该能够从磁盘执行此操作。

我通过WCF使用WCF服务,我从远程WSDL生成了代理客户端。

下面是我从网络收到的XML结构(它是用System.ServiceModel.MessageLogging记录的),我希望将其解析为生成的类CRResponse:

代码语言:javascript
复制
<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]">
...

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

我试着这样解析消息:

代码语言:javascript
复制
  GetCRResponse body;
  using (var xmlReader =  XmlReader.Create("Requests\\CR.xml"))
  {
      Message m = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap11);
      body = m.GetBody<GetCRResponse>();
  }

在GeyBody方法中,会引发此异常:

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

我尝试使用SoapFormatter:

代码语言:javascript
复制
using ( FileStream fs = new FileStream("Requests\\CR.xml", FileMode.Open) )
{
    SoapFormatter formatter = new SoapFormatter();
    body = (ActGetCRResponse)formatter.Deserialize(fs);
}

..the反序列化引发以下异常:分析错误,没有与XML键“”ns2 GetCRResponse“”关联的程序集。“”

我不能使用xml序列化程序将其反序列化为GetCRResponse,因为soap序列化程序需要解释SOAP-ENC:arrayType属性。

EN

回答 1

Stack Overflow用户

发布于 2017-04-29 05:48:33

更新:

代码语言:javascript
复制
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());

reference

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43682043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档