【问题标题】:C# SOAP client: sending generic XmlNode requestC# SOAP 客户端:发送通用 XmlNode 请求
【发布时间】:2019-03-19 16:58:38
【问题描述】:

我有一个 C# 项目,我在其中添加了一个 SOAP 服务引用,使用集成的 Visual Studio 功能(右键单击 -> 添加 -> 服务引用)

客户端类生成正确,没有错误。然而,服务的各种方法只接受一个通用的System.Xml.XmlNode 作为输入,而不是一个结构化的对象。

这在理论上应该不是问题,因为我有完整的 XML 文件,其中包含我需要执行的查询。所以我试着这样做:

NSIStdV20ServiceSoapClient client = new NSIStdV20ServiceSoapClient();
var getAllDataFlowQuery = File.ReadAllText(@"Query\get_all_dataflow.xml"); //file containing the query
XmlDocument doc = new XmlDocument();
doc.LoadXml(getAllDataFlowQuery);
var dataStructures = client.QueryStructure(doc); //this method accepts a System.Xml.XmlNode as parameter

但是,这不起作用,抛出

System.ServiceModel.FaultException: 'Error due to a non correct client message'

我最初认为查询不正确,但我尝试使用SoapUI 执行完全相同的查询,并且效果很好!我什至尝试使用 doc.InnerXml 返回的确切 XML 来执行此操作(只是为了确保 XmlDocument 对象没有修改 XML)并且它可以工作。

所以基本上只有从 C# 调用该方法时它才不起作用。

如果你想自己尝试一下,服务是免费的,WSDL在这里:

http://sdmx.istat.it/SDMXWS/NsiStdV20Service.asmx?WSDL

您应该尝试使用以下负载调用QueryStructure 方法:

<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://ec.europa.eu/eurostat/sri/service/2.0"><soapenv:Header /><soapenv:Body><web:QueryStructure><!--Optional:--><web:Query><RegistryInterface xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd" xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common" xmlns:compact="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/compact" xmlns:cross="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/cross" xmlns:generic="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic" xmlns:query="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/query" xmlns:structure="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure" xmlns:registry="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/registry" xmlns:utility="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/utility" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Header><ID>JD014</ID><Test>true</Test><Truncated>false</Truncated><Name xml:lang="en">Trans46302</Name><Prepared>2001-03-11T09:30:47-05:00</Prepared><Sender id="BIS" /></Header><QueryStructureRequest resolveReferences="false"><registry:DataflowRef /></QueryStructureRequest></RegistryInterface></web:Query></web:QueryStructure></soapenv:Body></soapenv:Envelope>

正如我所说,这在 SoapUI 中完美运行,但在从 C# 调用客户端方法时不起作用

【问题讨论】:

  • 你试过client.QueryStructure(doc.DocumentElement)吗?
  • @dbc:是的,但这不是问题,请参阅下面的答案。

标签: c# xml soap


【解决方案1】:

好吧,似乎由 Visual Studio 生成的客户端,即使它接受 XmlNode 作为输入,也会自己创建一些所需的外部结构(准确地说:所有带有 soapenvweb 的外部节点命名空间)。

这意味着我必须将输入 XML 剥离为:

<?xml version="1.0" encoding="UTF-8"?><RegistryInterface xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd" xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common" xmlns:compact="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/compact" xmlns:cross="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/cross" xmlns:generic="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic" xmlns:query="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/query" xmlns:structure="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/structure" xmlns:registry="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/registry" xmlns:utility="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/utility" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Header><ID>JD014</ID><Test>true</Test><Truncated>false</Truncated><Name xml:lang="en">Trans46302</Name><Prepared>2001-03-11T09:30:47-05:00</Prepared><Sender id="BIS" /></Header><QueryStructureRequest resolveReferences="false"><registry:DataflowRef /></QueryStructureRequest></RegistryInterface>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多